日期:2014-05-17 浏览次数:21196 次
SqlCommand cmd = new SqlCommand("insert into A([ID],[CODE]) values(@ID,@CODE)",cn);
cmd.Parameters.Add("@ID",SqlDbType.Int,4,"ID");
cmd.Parameters.Add("@CODE", SqlDbType.Uniqueidentifier, 16, "CODE");
//cmd.Parameters.Add("@CODE", SqlDbType.Varchar, 32, "CODE"); 这样写也是错的,错误提示“将字符串转换为uniqueidentifier时失败”
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = cmd;
DataTable dt=new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("CODE");
for(int i=1;i<=100;i++)
{
DataRow newRow = dt.NewRow();
newRow["ID"] = i;
newRow["CODE"] = Guid.NewGuid();
dt.Rows.Add(newRow);
}
cn.Open();
da.Update(dt);
cn.Close();
SqlCommand cmd = new SqlCommand("insert into A([ID],[CODE]) values(@ID,@CODE)",cn);
cmd.Parameters.Add(New SqlParameter("@ID",1));
cmd.Parameters.Add(New SqlParameter("@CODE",Guid.NewGuid());
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();