vs2008中单击按钮怎么写代码将数据添加到sql中的数据库?数据库我已经连接好!!
2个回答
展开全部
string SQLStr = "insert into users(uaccount,upwd,uemail,uname,uphone,uaddress)values(@uaccount,@upwd,@uemail,@uname,@uphone,@uaddress)";
SqlConnection conn=new SqlConnection(CString);
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = SQLStr;
SqlParameter[] paras = new SqlParameter[6];
paras[0] = new SqlParameter("@uaccount",SqlDbType.VarChar,50);
paras[0].Value = this.txtuserId.Text;
paras[1] = new SqlParameter("@upwd",SqlDbType.Char,50);
paras[1].Value = this.txtPsw.Text;
paras[2] = new SqlParameter("@uemail",SqlDbType.VarChar,50);
paras[2].Value = this.txtemail.Text;
paras[3] = new SqlParameter("@uname",SqlDbType.NVarChar,20);
paras[3].Value = this.tetusername.Text;
paras[4] = new SqlParameter("@uphone",SqlDbType.Char,20);
paras[4].Value = this.tetphone.Text;
paras[5] = new SqlParameter("@uaddress",SqlDbType.NVarChar,50);
paras[5].Value = this.tetaddress.Text;
for (int i = 0; i < paras.Length; i++)
{
com.Parameters.Add(paras[i]);
}
try
{
conn.Open();
}
catch
{
MyMessageBox.alert("数据库连接失败。", this.Page);
//this.Label1.Text = "数据库连接失败。";
return;
}
int k = com.ExecuteNonQuery();
if (k > 0)
MyMessageBox.alert("注册成功。", this.Page);
//this.Label1.Text = "注册成功。";
else
MyMessageBox.alert("注册失败。", this.Page);
//this.Label1.Text = "注册失败。";
conn.Close();
大概就是这样 因为不知道你的内容 稍作修改吧
SqlConnection conn=new SqlConnection(CString);
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = SQLStr;
SqlParameter[] paras = new SqlParameter[6];
paras[0] = new SqlParameter("@uaccount",SqlDbType.VarChar,50);
paras[0].Value = this.txtuserId.Text;
paras[1] = new SqlParameter("@upwd",SqlDbType.Char,50);
paras[1].Value = this.txtPsw.Text;
paras[2] = new SqlParameter("@uemail",SqlDbType.VarChar,50);
paras[2].Value = this.txtemail.Text;
paras[3] = new SqlParameter("@uname",SqlDbType.NVarChar,20);
paras[3].Value = this.tetusername.Text;
paras[4] = new SqlParameter("@uphone",SqlDbType.Char,20);
paras[4].Value = this.tetphone.Text;
paras[5] = new SqlParameter("@uaddress",SqlDbType.NVarChar,50);
paras[5].Value = this.tetaddress.Text;
for (int i = 0; i < paras.Length; i++)
{
com.Parameters.Add(paras[i]);
}
try
{
conn.Open();
}
catch
{
MyMessageBox.alert("数据库连接失败。", this.Page);
//this.Label1.Text = "数据库连接失败。";
return;
}
int k = com.ExecuteNonQuery();
if (k > 0)
MyMessageBox.alert("注册成功。", this.Page);
//this.Label1.Text = "注册成功。";
else
MyMessageBox.alert("注册失败。", this.Page);
//this.Label1.Text = "注册失败。";
conn.Close();
大概就是这样 因为不知道你的内容 稍作修改吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询