【发布时间】:2010-08-30 23:01:44
【问题描述】:
我有一个简单的两字段表单,将其数据存储在数据库中。由于某种原因,它不起作用。我已验证连接字符串有效,因为它在我制作的另一个项目中使用。
我没有包括第一个类的开始或其页面加载。
代码:
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();
}
}
【问题讨论】:
-
异常出现在哪里,是什么?
-
它实际上并没有产生错误,当我去查看表格时没有插入值。来了解一下,连接字符串是用引号括起来的,我没有使用ExecuteNonQuery()
-
没有回答你的问题,但是你的代码容易受到SQL injection的攻击,这是非常危险的。谷歌一下,开始使用参数化查询。
-
感谢您的建议,我会调查一下。
标签: c# asp.net .net sql sql-server