【问题标题】:is it possible to link a text box to a database?是否可以将文本框链接到数据库?
【发布时间】:2020-08-18 03:50:22
【问题描述】:

我目前正在处理我的 C#“WindowForm”项目,我想知道是否可以将文本框从任何表单链接到数据库并不时更新文本框内容,以便我的应用程序的用户能够获得我愿意以这种形式(文本框)发布的最新信息。

谢谢你,

【问题讨论】:

  • 我不明白为什么不这样做。创建一个数据库并将其连接到您的应用程序。将预期值推送到您认为合适的数据库中。
  • 您可以databind 文本框,但必须从您的应用程序触发更新数据。
  • 好的,谢谢大家。

标签: c# database visual-studio windows-forms-designer


【解决方案1】:

如果你想从数据表中实时刷新文本,你可以试试SqlDependency

以下是一个简单的演示。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    System.Data.SqlClient.SqlConnection conn = null;
    System.Data.SqlClient.SqlCommand command = null;
    // Set connection string
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder
    {
        DataSource = @"datasource name",
        // set database
        InitialCatalog = @"catalog name",
        // access the database using the existing windows security certificate
        IntegratedSecurity = true
    };

    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new System.Data.SqlClient.SqlConnection(builder.ConnectionString);
        command = conn.CreateCommand();
        command.CommandText = "select text from dbo.TableText where id<>20 order by id desc ";

        // Start
        SqlDependency.Start(builder.ConnectionString);
        // Get data
        GetData();
    }

    private void GetData()
    {
        command.Notification = null;
        SqlDependency dependency = new SqlDependency(command);
        dependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);

        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            System.Data.DataSet ds = new DataSet();
            adapter.Fill(ds, "test");
            string text = ds.Tables["test"].Rows[0]["Text"].ToString();
            textBox.Text = text;
        }
    }

    void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e)
    {
        // Because it is a child thread, you need to update the ui with the invoke method.
        if (this.InvokeRequired)
        {
            this.Invoke(new OnChangeEventHandler(sqlDependency_OnChange), new object[] { sender, e });
        }
        else
        {
            SqlDependency dependency = (SqlDependency)sender;
            dependency.OnChange -= sqlDependency_OnChange;
            // After the notification, the current dependency is invalid, you need to re-get the data and set the notification.
            GetData();
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        // Clear resource
        SqlDependency.Stop(builder.ConnectionString);
        conn.Close();
        conn.Dispose();
    }
}

更多关于SqlDependency的信息,可以参考Detecting Changes with SqlDependency

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多