c# 如何插入文本框值并将其保存到sql数据库?

发布于 2024-10-27 09:19:25 字数 1607 浏览 2 评论 0原文


如何插入文本框值并将其保存到sql数据库? 关于上述问题,我需要一些帮助。当我单击“保存”按钮时,它应该将输入文本框更新为 sql 数据库 Workers。你们可以制作一些编码示例来实现这一目标吗?因为我所做的根本不起作用。这是编码:

private void btnSave_Click(object sender, EventArgs e) {
#region SaveButton
            // System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ();

            //System.Data.SqlClient.SqlCommandBuilder cb;
            //cb = new System.Data.SqlClient.SqlCommandBuilder (da);

            //add to Dataset a new row
            DataRow dRow = ds1.Tables["Workers"].NewRow();

            //add data to the new row just have been created
            //refer to first_Name
            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;

            //add command
            //add to table worker a new row that declared by row variable name dRow
            ds1.Tables["Workers"].Rows.Add(dRow);

            MaxRows = MaxRows + 1; //to enable last row is still last row
            inc = MaxRows - 1;

            //call data adapter da to update and save data into database sql server
            //da.Update(ds1, "Workers");

            MessageBox.Show("Entry Added!");
#endregion
            con.ConnectionString = "Data Source=.\\SQLEXPRESS; AttachDbFilename =D:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";


            string strSQL = "INSERT INTO Workers (first_Name, last_Name, job_Title )" + " VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', " + " '" + textBox3.Text + "') ";





            con.Close();  
        }

How to insert textbox value and save it to sql database?
I need some help here regarding to the question above. When I clicked button save, it should update the input textbox to the sql database Workers. Could you guys make some coding sample to achieve this? Because what I do is not working at all. This is the coding :

private void btnSave_Click(object sender, EventArgs e) {
#region SaveButton
            // System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ();

            //System.Data.SqlClient.SqlCommandBuilder cb;
            //cb = new System.Data.SqlClient.SqlCommandBuilder (da);

            //add to Dataset a new row
            DataRow dRow = ds1.Tables["Workers"].NewRow();

            //add data to the new row just have been created
            //refer to first_Name
            dRow[1] = textBox1.Text;
            dRow[2] = textBox2.Text;
            dRow[3] = textBox3.Text;

            //add command
            //add to table worker a new row that declared by row variable name dRow
            ds1.Tables["Workers"].Rows.Add(dRow);

            MaxRows = MaxRows + 1; //to enable last row is still last row
            inc = MaxRows - 1;

            //call data adapter da to update and save data into database sql server
            //da.Update(ds1, "Workers");

            MessageBox.Show("Entry Added!");
#endregion
            con.ConnectionString = "Data Source=.\\SQLEXPRESS; AttachDbFilename =D:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";


            string strSQL = "INSERT INTO Workers (first_Name, last_Name, job_Title )" + " VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', " + " '" + textBox3.Text + "') ";





            con.Close();  
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-11-03 09:19:25

我通过正确连接到 Workers 数据库解决了这个问题。是啊!

这是这个问题的正确代码:

private void btnSave_Click(object sender, EventArgs e)
{
    #region SaveButton
    System.Data.SqlClient.SqlDataAdapter da;
    string sql = "SELECT * From tblWorkers";
    da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

    System.Data.SqlClient.SqlCommandBuilder cb;
    cb = new System.Data.SqlClient.SqlCommandBuilder (da);

    //add to Dataset a new row
    DataRow dRow = ds1.Tables["Workers"].NewRow();

    //add data to the new row that has just been created
    //refer to first_Name
    dRow[1] = textBox1.Text;
    dRow[2] = textBox2.Text;
    dRow[3] = textBox3.Text;

    //add command
    //add to table worker a new row that declared by row variable name dRow
    ds1.Tables["Workers"].Rows.Add(dRow);

    MaxRows = MaxRows + 1; //to enable last row is still last row
    inc = MaxRows - 1;

    //call data adapter da to update and save data into database sql server
    da.Update(ds1, "Workers");              

    MessageBox.Show("Entry Added!");
    con.Close();
    #endregion 

I have solved this question by connecting properly to the Workers database. YeaY!!

Here's the right code for this question:

private void btnSave_Click(object sender, EventArgs e)
{
    #region SaveButton
    System.Data.SqlClient.SqlDataAdapter da;
    string sql = "SELECT * From tblWorkers";
    da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

    System.Data.SqlClient.SqlCommandBuilder cb;
    cb = new System.Data.SqlClient.SqlCommandBuilder (da);

    //add to Dataset a new row
    DataRow dRow = ds1.Tables["Workers"].NewRow();

    //add data to the new row that has just been created
    //refer to first_Name
    dRow[1] = textBox1.Text;
    dRow[2] = textBox2.Text;
    dRow[3] = textBox3.Text;

    //add command
    //add to table worker a new row that declared by row variable name dRow
    ds1.Tables["Workers"].Rows.Add(dRow);

    MaxRows = MaxRows + 1; //to enable last row is still last row
    inc = MaxRows - 1;

    //call data adapter da to update and save data into database sql server
    da.Update(ds1, "Workers");              

    MessageBox.Show("Entry Added!");
    con.Close();
    #endregion 
一生独一 2024-11-03 09:19:25

您需要执行非查询

using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();

    }

You'll need to Execute non query

Source

using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文