SQL c# insert Command doesn't work
I'm having a problem insertin data into my database. I can read from the
database by using the select query, so I know my connection string is
correct, but for some reason the insert doesn't work. Here's my code:
private string ConnectionString()
{
return @"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\dbBusiness.mdf;Integrated
Security=True;User Instance=True";
}
private void Insert()
{
string sqlStrInsert = "INSERT INTO table
(param1,param2)VALUES(@param1,@param2)";
SqlConnection connection = new SqlConnection(ConnectionString());
SqlCommand command = new SqlCommand(sqlStrInsert, connection);
command.Parameters.Add("@param1", SqlDbType.SmallInt);
command.Parameters.Add("@param2", SqlDbType.NVarChar,50);
command.Parameters["@param1"].Value = numOf_company;
command.Parameters["@param2"].Value = txt_name.Text;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
Im having trouble figuring this out so i'd appreciate anyone who helps
No comments:
Post a Comment