【问题标题】:ADO.NET objects in C# not working properlyC# 中的 ADO.NET 对象无法正常工作
【发布时间】:2012-05-26 19:55:49
【问题描述】:
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\CustomersDB.mdf;Integrated Security=True;User Instance=True");

SqlCommand cmd = new SqlCommand("INSERT INTO Customers (ID, Date, GUIA, SName, SAddress, SCity, SState, SZipCode, SPhone, SEmail, RName, RAddress, RCity, RState, RZipCode, RPhone, REmail) VALUES (1,'"+textBox1.Text + "','" + textBox2.Text+"','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox16.Text + "','" + textBox15.Text + "','" + textBox14.Text + "','" + textBox13.Text + "','" + textBox12.Text + "','" + textBox11.Text + "','" + textBox10.Text +"')" , con);
cmd.CommandType = System.Data.CommandType.Text;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

MessageBox.Show("Data added successfully!");

如您所见,我正在尝试向数据库中添加一些数据,这些数据是在 C# Windows 窗体应用程序中创建的。

但是,执行代码后,我没有收到任何错误,但是当我查看表格数据时,没有任何变化。

换句话说,即使代码执行正确,也不会添加任何数据。

这里有什么缺陷?任何帮助表示赞赏。

【问题讨论】:

  • 您的 ID 列是 IDENTITY 列吗?
  • 那么,消息框是否显示消息?
  • 是的,ID 是一个标识列。
  • 为什么你认为你可以插入一个身份列?通常它是关闭的,不应该打开。

标签: c# sql sql-server database ado.net


【解决方案1】:

首先,我想指出你有一个巨大的 SQL 注入。其次,看看Rows not being updated,看看是不是和你一样的问题。

【讨论】:

  • 嗨。我知道 SQL 注入,但我想:有人如何将 SQL 语句注入桌面应用程序?
  • 但是没有用!我改变了财产,仍然是同样的事情。我尝试通过右键单击“服务器资源管理器”选项卡中的表并选择“显示表数据”来查看数据。此外,我正在使用的 gridview 链接到同一个数据库,它不显示正在添加的新数据。
  • @V0R73X “嗨。我知道 SQL 注入,但我想:有人如何将 SQL 语句注入桌面应用程序?”饼干无处不在
【解决方案2】:

主要缺陷是整个 User Instance 和 AttachDbFileName= 方法。 Visual Studio 将复制 .mdf 文件,很可能,您的 INSERT 工作正常 - 但您只是查看 错误的 .mdf 文件结束!

如果您想坚持使用这种方法,请尝试在 myConnection.Close() 调用上设置断点 - 然后使用 SQL Server Mgmt Studio Express 检查 .mdf 文件 - 我几乎可以肯定您的数据在那里。

在我看来,真正的解决方案

  1. 安装 SQL Server Express(反正你已经完成了)

  2. 安装 SQL Server Management Studio Express

  3. SSMS Express 中创建您的数据库,并为其命名(例如 CustomersDB

  4. 使用它的逻辑数据库名称(在服务器上创建它时给出)连接到它——不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:

    Data Source=.\\SQLEXPRESS;Database=CustomersDB;Integrated Security=True
    

    其他一切都一模一样和以前一样......

【讨论】:

  • 我对管理工作室真的不满意,安装 SQL 框架后它没有安装。它们必须完全安装,否则以后无法添加 Management Studio。管理工作室也有好几个版本,不知道哪一个适合我。上次我花了几个小时寻找合适的版本,最后我发现我无法安装它。
  • 好吧,我只是坚信这种“附加数据库文件”方法在 SQL Server Express 等基于服务器的产品中存在根本缺陷。您正在与系统作斗争 - 而不是拥抱它并充分利用它!如果您不需要 RDBMS 服务器的强大功能 - 那么您应该查看 SQLite 或 SQL Server Compact Edition 之类的东西,它们是基于文件的、基本上是单用户、本地计算机类型的“数据库”系统跨度>
  • 在不使用管理工作室的情况下,还有其他注册数据库的方法吗?也许一些 T-SQL 代码...
  • @V0R73X:如果你坚持不使用 Mgmt Studio,你总是可以在命令行上使用SQLCMD.exe ...
  • @V0R73X - 你开始熟悉你应该使用的工具怎么样?至少有点半专业的感觉。抱怨“我真的不习惯”(在此处插入随机专业系统工具)不是一个好方法。
【解决方案3】:

1 您的查询将创建SQL Injection,请尝试使用 SP 或 LINQ 以更安全地执行。

[2] 首先尝试用你的 sql server 数据库表执行你的长查询返回字符串值,因为这里你没有显示你的表结构,所以任何单引号都不会执行正确的查询。

string sqlstr = "INSERT INTO Customers (ID, Date, GUIA, SName, SAddress, SCity, SState, SZipCode, SPhone, SEmail, RName, RAddress, RCity, RState, RZipCode, RPhone, REmail) VALUES (1,'"+textBox1.Text + "','" + textBox2.Text+"','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox16.Text + "','" + textBox15.Text + "','" + textBox14.Text + "','" + textBox13.Text + "','" + textBox12.Text + "','" + textBox11.Text + "','" + textBox10.Text +"')"

[3] 最后一点更好的命名对编码很重要。

【讨论】:

    【解决方案4】:
    cn.ConnectionString = @"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\CustomersDB.mdf;Integrated Security=True;User Instance=True";
                cn.Open();
                SqlCommand com = new SqlCommand();
                com.Connection = cn;
                com.CommandType = CommandType.Text;
    
                com.CommandText = "INSERT INTO Customers (ID, Date, GUIA, SName, SAddress,               SCity, SState, SZipCode, SPhone, SEmail, RName, RAddress, RCity, RState, RZipCode, RPhone, REmail) VALUES (1,'"+textBox1.Text + "','" + textBox2.Text+"','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox16.Text + "','" + textBox15.Text + "','" + textBox14.Text + "','" + textBox13.Text + "','" + textBox12.Text + "','" + textBox11.Text + "','" + textBox10.Text +"')" ;
              com.ExecuteNonQuery();
              MessageBox.Show("Saving is done!");
    

    试试这个代码,看看它是否工作我认为这应该工作.. ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多