在这里,我在数据库中动态创建表。用户根据自己的意愿输入名称并选择语言单选按钮。所以问题是在执行 cmd.ExecuteNonQuery 之后,我整数的值从 0 变为 -1。并显示该表无法创建,但是当我转到数据库时,它已经成功创建。请让我知道我在哪里做错了。提前感谢!
protected void btnpaper_Click(object sender, EventArgs e)
{
try
{
string conn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
con.Open();
char[] arr = new char[] {'n','g','l','i','s','h'};
string str = "CREATE TABLE " + Label1.Text.Trim() +
txtpaperset.Text.Trim()+ rbtnEng.Text.TrimEnd(arr) +
"(" + "quesNo int NOT NULL PRIMARY KEY, " +
"question varchar(1000) NOT NULL," +
"ansA varchar(500) NOT NULL, " +
"ansB varchar(500) NOT NULL, " +
"ansC varchar(500) NOT NULL, " +
"ansD varchar(500) NOT NULL, " +
"rightAns varchar(50) NOT NULL " + ")";
SqlCommand cmd = new SqlCommand(str, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
lblerrormsg.Visible = true;
con.Close();
}
else
{
lblerrormsg.Text = "Table Not Created Please Try with Different Name!";
con.Close();
}
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}
}