[英]How do I access a database in C#
基本上,我想簡要說明如何使用C#代碼訪問SQL數據庫。 我認為需要連接和命令,但是發生了什么? 我想我要問的是有人讓這個過程神秘化了一下。 謝謝。
為清楚起見,在我的情況下,我正在做網絡應用程序,電子商務的東西。 它是所有ASP.NET,C#和SQL數據庫。
我要繼續關閉這個帖子。 這是一般性的,我將發布一些更尖銳和教程式的問題和答案的主題。
MSDN在這里寫得非常好:
http://msdn.microsoft.com/en-us/library/s7ee2dwt(VS.71).aspx
您應該查看數據讀取器以獲取簡單的select語句。 來自MSDN頁面的示例:
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(
queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}",
reader[0], reader[1]));
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
}
它基本上首先創建一個SqlConnection對象,然后創建SqlCommand -object,它保存您要執行的實際選擇,以及對我們剛剛創建的連接的引用。 然后它打開連接,在下一行,執行您的語句並返回一個SqlDataReader對象。
在while循環中,它然后輸出閱讀器中第一行的值。 每次調用“reader.Read()”時,閱讀器都會包含一個新行。
然后讀者關閉,因為我們退出“使用” - 秘密,連接也被關閉。
編輯:如果您正在尋找有關在ASP.NET中選擇/更新數據的信息,4GuysFromRolla 在ASP.NET 2.0的數據源控件上有一個非常好的Multipart系列
編輯2:正如其他人所指出的,如果您使用的是較新版本的.NET,我建議您查看LINQ。 可在此MSDN頁面上找到介紹,示例和寫入。
舊的ADO.Net(sqlConnection等)是LINQ出現的恐龍。 LINQ需要.Net 3.5,但向后兼容所有.Net 2.0+和Visual Studio 2005等。
從linq開始是非常容易的。
您現在已經構建了一些類。 您構建了exampleDataContext類,它是您的linq初始化程序,並且您構建了item類,它是items表中對象的類。 這一切都是自動完成的,您無需擔心。 現在說我想獲得itemID為3的記錄,這就是我需要做的:
exampleDataContext db = new exampleDataContext(); // initializes your linq-to-sql
item item_I_want = (from i in db.items where i.itemID == 3 select i).First(); // using the 'item' class your dbml made
這就是全部。 現在你有了一個名為item_I_want的新項目 ...現在,如果你想從項目中獲得一些信息,你可以這樣稱呼它:
int intID = item_I_want.itemID;
string itemName = item_I_want.name;
Linq使用起來非常簡單! 這只是冰山一角。
當你擁有一個更強大,更簡單的工具時,無需學習過時的ADO :)
要看的主題:
如果它是一個Web應用程序,這里有一些很好的資源來開始.NET中的數據訪問:
http://weblogs.asp.net/scottgu/archive/2007/04/14/working-with-data-in-asp-net-2-0.aspx
在SQL Server db上連接/執行操作:
using System.Data;
using System.Data.SqlClient;
string connString = "Data Source=...";
SqlConnection conn = new SqlConnection(connString); // you can also use ConnectionStringBuilder
connection.Open();
string sql = "..."; // your SQL query
SqlCommand command = new SqlCommand(sql, conn);
// if you're interested in reading from a database use one of the following methods
// method 1
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
object someValue = reader.GetValue(0); // GetValue takes one parameter -- the column index
}
// make sure you close the reader when you're done
reader.Close();
// method 2
DataTable table;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
// then work with the table as you would normally
// when you're done
connection.Close();
大多數其他數據庫服務器(如MySQL和PostgreSQL)具有類似的連接和操作接口。
如果您正在尋找的是一個易於學習的教程,那么您應該訪問www.ASP.net網站。
以下是入門視頻頁面的鏈接: http : //www.asp.net/learn/videos/video-49.aspx
以下是視頻,如果您想下載它: 視頻下載
這里是視頻: 下載項目的C#項目鏈接
祝好運。
我還建議使用DataSet。 它們非常易於使用,只需點擊幾下鼠標,無需編寫任何代碼,對於小型應用程序而言也足夠好。
如果您有Visual Studio 2008,我建議您跳過ADO.NET並直接跳到LINQ to SQL
@JD OConal基本上是正確的,但你需要確保你處理你的連接:
string connString = "Data Source=...";
string sql = "..."; // your SQL query
//this using block
using( SqlConnection conn = new SqlConnection(connString) )
using( SqlCommand command = new SqlCommand(sql, conn) )
{
connection.Open();
// if you're interested in reading from a database use one of the following methods
// method 1
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
object someValue = reader.GetValue(0); // GetValue takes one parameter -- the column index
}
// make sure you close the reader when you're done
reader.Close();
// method 2
DataTable table;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
// then work with the table as you would normally
// when you're done
connection.Close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.