我想向表中添加记录,我的代码如下:
string StrSql;
SqlConnection MyConnection1=new SqlConnection("server=(local)\\HJHSQL;database=Materials;uid=hjh;pwd=911144");
StrSql="INSERT INTO Users VALUES(@UserName,@Email,@Password)";
SqlCommand MyCommand1=new SqlCommand(StrSql,MyConnection1);
MyCommand1.Parameters.Add(new SqlParameter("@UserName",SQLDataType.varchar,20));
MyCommand1.Parameters["@UserName"].Value=this.TextBoxUserName.Text;
MyCommand1.Parameters.Add(new SqlParameter("@Email",SQLDataType.varchar,50));
MyCommand1.Parameters["@Email"].Value=this.TextBoxEmail.Text;
MyCommand1.Parameters.Add(new SqlParameter("@PassWord",SQLDataType.varchar,20));
MyCommand1.Parameters["@PassWord"].Value=this.TextBoxPassWord.Text;
MyCommand1.Connection.Open();
MyCommand1.ExecuteNonQuery();
MyCommand1.Connection.Close();但是SQLDataType不被识别,但是如果不定义数据的类型的话,SQL Server有会给出类型不匹配的提示
那应该用什么方法来定义这几个变量使他们与表中类型一致呢???谢谢
string StrSql;
SqlConnection MyConnection1=new SqlConnection("server=(local)\\HJHSQL;database=Materials;uid=hjh;pwd=911144");
StrSql="INSERT INTO Users VALUES(@UserName,@Email,@Password)";
SqlCommand MyCommand1=new SqlCommand(StrSql,MyConnection1);
MyCommand1.Parameters.Add(new SqlParameter("@UserName",SQLDataType.varchar,20));
MyCommand1.Parameters["@UserName"].Value=this.TextBoxUserName.Text;
MyCommand1.Parameters.Add(new SqlParameter("@Email",SQLDataType.varchar,50));
MyCommand1.Parameters["@Email"].Value=this.TextBoxEmail.Text;
MyCommand1.Parameters.Add(new SqlParameter("@PassWord",SQLDataType.varchar,20));
MyCommand1.Parameters["@PassWord"].Value=this.TextBoxPassWord.Text;
MyCommand1.Connection.Open();
MyCommand1.ExecuteNonQuery();
MyCommand1.Connection.Close();但是SQLDataType不被识别,但是如果不定义数据的类型的话,SQL Server有会给出类型不匹配的提示
那应该用什么方法来定义这几个变量使他们与表中类型一致呢???谢谢
SQLDataType.varchar //是语法没通过?注意大小写改为SqlDbType.VarChar
SqlConnection MyConnection=new SqlConnection(MyConnectionString);
SqlCommand MyCommand=new SqlCommand();
MyCommand.CommandType=CommandType.StoredProcedure;
MyCommand.CommandText="[M_Product_AddSubItemCategory]";
MyCommand.Connection=MyConnection;
MyCommand.Parameters.Add("@SubItemCategoryID",this.txtID.Text.Trim());
MyCommand.Parameters.Add("@SubItemCategoryName",this.txtName.Text.Trim());
MyCommand.Parameters.Add("@MainItemCategoryID",this.drownlistMainItemCategoryID.SelectedItem.Value.ToString());
MyCommand.Parameters.Add("@Description",this.txtDescription.Text.Trim());
SqlParameter Parameter=MyCommand.Parameters.Add("@Result",SqlDbType.VarChar,20);
Parameter.Direction=ParameterDirection.Output;
MyConnection.Open();
MyCommand.ExecuteNonQuery();
string Result=MyCommand.Parameters["@Result"].Value.ToString();
if(Result=="exits")
{
this.txtMessage.Visible=true;
this.txtMessage.Text="The SubItemCategoryID has exits !";
}
else if(Result=="true")
{
DataBindDataGrid();
this.txtMessage.Visible=true;
this.txtMessage.Text="Add Succesful !";
}
MyConnection.Close();