简体   繁体   English

为什么这个INSERT命令不起作用?

[英]Why doesn't this INSERT command work?

My system does not come with any errors, but there is not added anything to tblHardware when I try to add. 我的系统没有任何错误,但是当我尝试添加时,没有添加任何内容到tblHardware。 This is my code. 这是我的代码。

private void btnTilfoj_Click(object sender, EventArgs e)
{
    conn = new SqlConnection(connectionstring);
    conn.Open();
    commandstring = "INSERT INTO tblItUdstyr([HardwareType], [KobsDato], [SerieNr]) VALUES(@HardwareType, @KobsDato, @SerieNr)";
    comm = new SqlCommand(commandstring, conn);

    comm.Parameters.Add("@HardwareType", SqlDbType.VarChar);
    comm.Parameters["@HardwareType"].Value = cbHardware.Text.ToString();

    comm.Parameters.Add("@KobsDato", SqlDbType.Date);
    comm.Parameters["@KobsDato"].Value = dtpKobsDato.Value;

    comm.Parameters.Add("@SerieNr", SqlDbType.VarChar);
    comm.Parameters["@SerieNr"].Value = txtSerienr.Text.ToString();
}

You need to run this: 你需要运行这个:

comm.ExecuteNonQuery();

However, there are some other things I'd recommend doing: 但是,还有一些我建议做的事情:

using (SqlConnection conn = new SqlConnection(connectionstring))
{
    conn.Open();

    commandstring = "INSERT INTO tblItUdstyr([HardwareType], [KobsDato], [SerieNr]) VALUES(@HardwareType, @KobsDato, @SerieNr)";
    comm = new SqlCommand(commandstring, conn);

    comm.Parameters.AddWithValue("@HardwareType", cbHardware.Text.ToString());
    comm.Parameters.AddWithValue("@KobsDato", dtpKobsDato.Value);
    comm.Parameters.AddWithValue("@SerieNr", txtSerienr.Text.ToString());

    comm.ExecuteNonQuery();
}

I added the using because that will properly close and dispose the SqlConnection . 我添加了using因为它将正确关闭并处置SqlConnection Further, I added the AddWithValue method because it's streamlined and more accurate as far as type matching. 此外,我添加了AddWithValue方法,因为它的流线型和类型匹配更准确。

Every command should be executed in some way. 每个命令都应该以某种方式执行。
Your code is fine, but lacks of the last call 你的代码很好,但没有最后一个电话

Use something like this 使用这样的东西

 commandstring = "INSERT INTO tblItUdstyr([HardwareType], [KobsDato], [SerieNr]) " + 
                 "VALUES(@HardwareType, @KobsDato, @SerieNr)";
using(conn = new SqlConnection(connectionstring))
using(comm = new SqlCommand(commandstring, conn))
{
     conn.Open();
     comm.Parameters.Add("@HardwareType", SqlDbType.VarChar);
     comm.Parameters["@HardwareType"].Value = cbHardware.Text.ToString();
     comm.Parameters.Add("@KobsDato", SqlDbType.Date);
     comm.Parameters["@KobsDato"].Value = dtpKobsDato.Value;
     comm.Parameters.Add("@SerieNr", SqlDbType.VarChar);
     comm.Parameters["@SerieNr"].Value = txtSerienr.Text.ToString();
     comm.ExecuteNonQuery();  //This is the call that sends your data to the database
}

Notice how I have put all your code inside a using statement . 请注意我是如何将所有代码放在using语句中的 This will enforce the closing and disposing of the SqlCommand and SqlConnection after you have finished. 这将在您完成后强制关闭和处理SqlCommand和SqlConnection。 Also in case of exceptions 如果有例外情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM