我在数据库中动态创建一个表。用户根据需要输入他们的姓名并使用RadioButton
. 问题是执行后cmd.ExecuteNonQuery
的值i
变为-1
from 0
。这表明无法创建表,但是当我转到数据库时,它已成功创建。请让我知道我哪里出错了?
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);
}
}