我想把变量ordid,num,proprice,proname和常量1插入一个表中,这样写对吗?
////////////////////////////////////////////////////////////////////////////////// SqlConnection insertconn = new SqlConnection(connectionString);
SqlCommand icmd= new SqlCommand();
icmd.CommandText="insert into detail(ord_id,pro_id,det_price,det_name,det_num) Values ('ordid','num','proprice','proname','1')";
//"INSERT Region (RegionID, RegionDescription) VALUES (5, 'NorthWestern')";
icmd.CommandType=CommandType.Text;
icmd.Connection=insertconn;
insertconn.Open();
icmd.ExecuteNonQuery();
insertconn.Close();
////////////////////////////////////////////////////////////////////////////////// SqlConnection insertconn = new SqlConnection(connectionString);
SqlCommand icmd= new SqlCommand();
icmd.CommandText="insert into detail(ord_id,pro_id,det_price,det_name,det_num) Values ('ordid','num','proprice','proname','1')";
//"INSERT Region (RegionID, RegionDescription) VALUES (5, 'NorthWestern')";
icmd.CommandType=CommandType.Text;
icmd.Connection=insertconn;
insertconn.Open();
icmd.ExecuteNonQuery();
insertconn.Close();
但是
icmd.CommandText="insert into detail(ord_id,pro_id,det_price,det_name,det_num) Values ('ordid','num','proprice','proname','1')";
插入的时候要注意 数据库类型 字符格式 才能用 '' 像:如:det_num要是int 直接写1就成
cmd.CommandText = "INSERT Region (RegionID, RegionDescription) VALUES (5, @param)";cmd.Parameter.Add("@param",SqlDbType.Varchar).Value = 变量;这样做的好