我有一个简单的两字段形式,它的数据存储在数据库中。出于某种原因,它不起作用。我已经验证了连接字符串的工作原理,因为它在我创建的另一个项目中使用。SQL Server插入
我没有包含第一类或其页面加载的开始。
代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string Name = txtName.Text;
string Description = txtSpecial.Text;
string method = string.Format(
"INSERT INTO RbSpecials (Name,Description,Active) VALUES ('{0}','{1}','1')",
Name,
Description);
RbConfiguration mySql = new RbConfiguration();
try
{
mySql.Sql_Connection(method);
}
catch
{
}
}
}
public class RbConfiguration
{
string DbConnectionString = "System.Configuration.ConfigurationManager.ConnectionStrings['RBConnectionString'].ConnectionString";
public void Sql_Connection(string queryString)
{
SqlConnection conn = new SqlConnection(DbConnectionString);
SqlCommand cmd = new SqlCommand(queryString, conn);
conn.Open();
conn.Close();
}
}
哪里例外出现,又是什么呢? – 2010-08-30 23:03:31
它实际上并没有产生错误,当我去查看表中没有插入值。来发现,连接字符串被括在引号中,我没有使用ExecuteNonQuery() – 2010-08-30 23:13:10
不回答你的问题,但你的代码易受[SQL注入](http://en.wikipedia.org/wiki/ SQL_injection),这是非常危险的。谷歌它,并开始使用参数化查询。 – 2010-08-31 01:46:19