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

时间:2021-07-08 07:03:51

My system does not come with any errors, but there is not added anything to tblHardware when I try to add. This is my code.

我的系统没有任何错误,但是当我尝试添加时,没有添加任何内容到tblHardware。这是我的代码。

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

2 个解决方案

#1


2  

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. This will enforce the closing and disposing of the SqlCommand and SqlConnection after you have finished. Also in case of exceptions

请注意我是如何将所有代码放在using语句中的。这将在您完成后强制关闭和处理SqlCommand和SqlConnection。如果有例外情况

#2


2  

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. Further, I added the AddWithValue method because it's streamlined and more accurate as far as type matching.

我添加了使用,因为它将正确关闭并处置SqlConnection。此外,我添加了AddWithValue方法,因为它的流线型和类型匹配更准确。

#1


2  

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. This will enforce the closing and disposing of the SqlCommand and SqlConnection after you have finished. Also in case of exceptions

请注意我是如何将所有代码放在using语句中的。这将在您完成后强制关闭和处理SqlCommand和SqlConnection。如果有例外情况

#2


2  

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. Further, I added the AddWithValue method because it's streamlined and more accurate as far as type matching.

我添加了使用,因为它将正确关闭并处置SqlConnection。此外,我添加了AddWithValue方法,因为它的流线型和类型匹配更准确。