每次都是这个地方出错,不知道是什么原因,是不是没连接到数据库,可是我直接登录到数据库是OK的,请高手帮忙。
谢谢!
int myQQNum = 0;
string message;
string sex = rbtnMale.Checked ? rbtnMale.Text : rbtnFemale.Text;
string sql = string.Format("insert into tb_User (Pwd,NickName,Sex,Age,Name,Star,BloodType) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}'); select @@Identity from tb_User", txtPwd.Text.Trim(), txtNickName.Text.Trim(),sex,int.Parse(txtAge.Text.Trim()),txtName.Text.Trim(),cboxStar.Text,cboxBloodType.Text);
SqlCommand command = new SqlCommand(sql,DataOperator.connection);
DataOperator.connection.Open();
int result = command.ExecuteNonQuery();
if (result==1)
{
sql = "select SCOPE_IDENTITY from tb_User";
myQQNum = Convert.ToInt32(command.ExecuteScalar());
message = string.Format("注册成功!你的MyQQ号码是" + myQQNum);
}
else
{
message = "注册失败,请重试";
}
DataOperator.connection.Close();
//下面是我定义的DataOperator类
class DataOperator
{
private static string connString = @"Data Source=7BULZ1BTUWZ2DZ9\\MR2014;Database=db_myqq;UID=sa;Pwd=;";
public static SqlConnection connection = new SqlConnection(connString);
public int ExecSQL(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int num = Convert.ToInt32(command.ExecuteScalar());
connection.Close();
return num;
}
public int ExecSQLResult(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int result = command.ExecuteNonQuery();
connection.Close();
return result;
}
public DataSet GetDataSet(string sql)
{
SqlDataAdapter sqlda = new SqlDataAdapter(sql,connection);
DataSet ds = new DataSet();
sqlda.Fill(ds);
return ds;
}
}
lilijie0404 发表于2018-04-07 11:23
//下面是我定义的DataOperator类
class DataOperator
{
private static string connString = @"Data Source=7BULZ1BTUWZ2DZ9\\MR2014;Database=db_myqq;UID=sa;Pwd=;";
public static SqlConnection connection = new SqlConnection(connString);
public int ExecSQL(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int num = Convert.ToInt32(command.ExecuteScalar());
connection.Close();
return num;
}
public int ExecSQLResult(string sql)
{
SqlCommand command = new SqlCommand(sql, connection);
if (connection.State == ConnectionState.Closed)
connection.Open();
int result = command.ExecuteNonQuery();
connection.Close();
return result;
}
public DataSet GetDataSet(string sql)
{
SqlDataAdapter sqlda = new SqlDataAdapter(sql,connection);
DataSet ds = new DataSet();
sqlda.Fill(ds);
return ds;
}
}
xxxxxxxxxxxxxxxxx