Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#如何插入文本框值并将其保存到sql数据库?_C#_.net_Database_Visual Studio 2005_Insert Update - Fatal编程技术网

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

c#如何插入文本框值并将其保存到sql数据库?,c#,.net,database,visual-studio-2005,insert-update,C#,.net,Database,Visual Studio 2005,Insert Update,如何插入文本框值并将其保存到sql数据库? 关于上述问题,我需要一些帮助。当我单击按钮save时,它应该将输入文本框更新为sql数据库工作者。你们能做一些编码样本来实现这一点吗?因为我所做的根本不起作用。这是编码: private void btnSave_Click(object sender, EventArgs e) { #region SaveButton // System.Data.SqlClient.SqlDataAdapter da = new Syste


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

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();  
        }

您需要执行非查询


通过正确连接到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 

你什么也没做。难怪它不起作用;-)。。。sql注入攻击就是这样开始的。按钮保存应该将其保存到数据库中,并永久更新。由于使用insert不起作用,有没有关于如何执行此操作的建议?da.Update(ds1,“Workers”)@justinlabenne关于如何为这个按钮使用sql命令有什么建议吗?对于初学者来说,您需要决定是使用dataadapter还是自己编写sql。
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