簡體   English   中英

c#如何插入文本框值並將其保存到sql數據庫?

[英]c# How to insert textbox value and save it to sql database?


如何插入文本框值並將其保存到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();  
        }

我已經通過正確連接到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 

您需要執行非查詢

資源

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM