【问题标题】:How can I see the data of a SQL server database in c#, and where it's store?如何在 C# 中查看 SQL Server 数据库的数据,以及它的存储位置?
【发布时间】:2009-11-29 16:18:34
【问题描述】:

我有一个用 C# Visual Studio 2008 编写的程序,带有 SQL Server 2005 (.mdf) 数据库。
以下是部分代码:

        ...   
        SqlCommandBuilder cb;
        cb = new SqlCommandBuilder(dataAdapter);
        String[] dataList =new String [8];
        ...
        DataTable resultTable = new DataTable();           
        FillDataList(data);
        dataAdapter = new SqlDataAdapter("insert into ProcessData values('" + dataList[0] + "','" + dataList[1] + "','" + dataList[2] + "','" + dataList[3] + "','" + dataList[4] + "','" + dataList[5] + "','" + dataList[6] + "','" + dataList[7]+"')", con);
        dataAdapter.Fill(resultTable); 
        ...

我的问题是:
1) 我在这些行中添加的数据存储在哪里?
2)为什么我在Server Explorer->Data Connections->Tables->ProcessData(我的表名)->“显示表数据”中用鼠标右键单击时,我看不到数据而只是列中为 NULL,如何查看那里的数据?
3) 为什么当我在 DataGridView 中显示这些数据时,有时会显示数据,有时却不显示?
非常感谢!

【问题讨论】:

  • 仅供参考,通过将字符串连接在一起来构建 SQL 命令不是一个好主意;您可能会让您的应用程序受到 SQL 注入攻击。您应该改用参数化 SQL 或存储过程。

标签: c# sql-server database


【解决方案1】:

没有看到关于 datalist 对象及其构造的更多信息,这是不可能的。但是,您错误地使用了 Fill 和 SQLDataAdapter。 FILL 依赖于具有 SelectCommand 属性集的 SQLDataAdapter,这是您的代码将 INSERT 语句插入的内容(这就是 SQLDataAdapter 的构造函数所做的)。所以......你的 Fill 什么也不返回,因为应该有的地方没有 SELECT。

您的 INSERT 应该是 dataadapter 的 InsertCommand 的一部分,并且您需要编写单独的 SELECT 语句才能将任何内容放入您的 resultTable。

【讨论】:

  • 我添加了缺少的代码,希望对您有所帮助。关于你所说的,也许我没有正确解释我的问题。我确实从数据库中获取数据,但有时它不保存数据。谢谢!
【解决方案2】:

我认为在编写更多代码之前,您需要阅读有关 ADO.NET 的一些基本文档。您将 INSERT 语句传递给 SqlDataAdapter 构造函数,该构造函数采用 SELECT 语句。您正在使用 SqlDataAdapter 的 Fill 方法,而您应该使用 Update 方法。您正在构建一个带有硬编码值的 SQL 字符串,您应该在其中使用 SqlParameters 和对 DataTable 列的引用。您的 DataTable 被称为“data”但 SQL 字符串使用“dataList”中的索引属性只是一个错字吗?因为如果是,DataTable 就没有索引属性。

【讨论】:

    【解决方案3】:

    尝试这样做(但您需要填写正确的连接字符串):

    // Get the db connection
    SqlConnection dbCon = new SqlConnection("connection string");
    
    // Select the data from the database table into a DataSet (even if it's empty)
    DataSet myData = new DataSet();
    SqlDataAdapter dbAdapter = new SqlDataAdapter("select * from ProcessData", dbCon);
    dbAdapter.Fill(myData);
    myData.Tables[0].TableName = "ProcessData"; // Keeps the table name consistent for the DataMember property
    
    // Use the command builder to add insert, delete, update commands to your adapter
    // You must have a primary key on the table for these to work, though
    SqlCommandBuilder dbComBuilder = new SqlCommandBuilder(dbAdapter);
    
    dbAdapter.InsertCommand = dbComBuilder.GetInsertCommand();
    dbAdapter.DeleteCommand = dbComBuilder.GetDeleteCommand();
    dbAdapter.UpdateCommand = dbComBuilder.GetUpdateCommand();
    
    // Bind the data set to the GridView for viewing / editing
    yourGridControl.AutoGenerateColumns = true; // Optional, if you haven't manually added the columns
    yourGridControl.DataSource = myData;
    yourGridControl.DataMember = "ProcessData";
    
    // Use the db adapter to update the database (by calling those commands) with 
    // changes made to the DataSet through the grid.  This would go in a different
    // form event, like a Save Button click.
    dbAdapter.Update();
    

    如果 ProcessData 表中没有主键,则可以使用以下命令直接插入值:

    // Insert the data directly with a command
    SqlCommand dbInsCommand = new SqlCommand("insert into ProcessData values (" + val1 + "," + val2 + ")", dbCon);
    dbInsCommand.ExecuteNonQuery();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-01
      • 2013-03-09
      • 1970-01-01
      • 2017-12-04
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多