簡體   English   中英

如何在c#中查看SQL Server數據庫的數據及其存儲位置?

[英]How can I see the data of a SQL server database in c#, and where it's store?

我有一個用SQL Server 2005(.mdf)數據庫在c#visual studio 2008中編寫的程序。
這是代碼的一部分:

        ...   
        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(我的表的名稱)->“ show Table Data”時,我看不到數據,而只是看到列中為NULL,如何查看那里的數據?
3)為什么當我在DataGridView中顯示此數據時,有時卻顯示數據而有時卻不顯示?
非常感謝!

如果沒有看到有關數據列表對象及其構造的更多信息,就很難說了。 但是,您使用的Fill和SQLDataAdapter錯誤。 FILL依賴於具有SelectCommand屬性集的SQLDataAdapter,這是您的代碼將INSERT語句綁定到的內容(這就是SQLDataAdapter的構造函數所做的事情)。 所以...您的Fill不返回任何內容,因為應該在沒有SELECT的地方進行。

INSERT應該是dataadapter的InsertCommand的一部分,並且您需要編寫一個單獨的SELECT語句才能將任何內容放入resultTable。

我認為在編寫更多代碼之前,您需要閱讀ADO.NET的一些基本文檔。 您正在將INSERT語句傳遞給SqlDataAdapter構造函數,該構造函數采用SELECT語句。 您正在使用SqlDataAdapter的Fill方法,而應該使用Update方法。 您正在構建帶有硬編碼值的SQL字符串,在該字符串中應使用SqlParameters和對DataTable Columns的引用。 事實是您的DataTable被稱為“數據”,而SQL字符串使用的是“ dataList”中的索引屬性只是一個錯字? 因為如果是,則DataTable沒有索引屬性。

嘗試這樣操作(盡管您需要填寫正確的連接字符串):

// 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM