2
A
回答
4
假設你使用Visual Studio作爲你的IDE,你可以使用LINQ to SQL。這是一種非常簡單的與數據庫交互的方式,它應該很快就可以開始。
Using LINQ to SQL是一個非常簡單的通過啓動和運行。
2
有MSDN tutorial on Creating Data Applications的讀取。您可能能夠澄清您的問題,或找到您需要的答案。
有關於editing the data in the app的信息,但您必須首先連接並將其加載到您的應用程序中。
2
在C#中這樣做的唯一原因是,如果您想以某種方式自動執行此操作,或者爲非技術用戶創建一個接口來與數據庫進行交互。您可以使用帶有SQL數據源的GridView控件來操作數據。
@kevin:如果他只是在學習,我認爲讓他使用SQLCommand對象(或SQLDataAdapter)可能更簡單。
10
第一步是創建一個連接。連接需要一個連接字符串。您可以使用SqlConnectionStringBuilder
創建連接字符串。
SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
connBuilder.InitialCatalog = "DatabaseName";
connBuilder.DataSource = "ServerName";
connBuilder.IntegratedSecurity = true;
然後使用連接字符串創建連接,像這樣:
SqlConnection conn = new SqlConnection(connBuilder.ToString());
//Use adapter to have all commands in one object and much more functionalities
SqlDataAdapter adapter = new SqlDataAdapter("Select ID, Name, Address from myTable", conn);
adapter.InsertCommand.CommandText = "Insert into myTable (ID, Name, Address) values(1,'TJ', 'Iran')";
adapter.DeleteCommand.CommandText = "Delete From myTable Where (ID = 1)";
adapter.UpdateCommand.CommandText = "Update myTable Set Name = 'Dr TJ' Where (ID = 1)";
//DataSets are like arrays of tables
//fill your data in one of its tables
DataSet ds = new DataSet();
adapter.Fill(ds, "myTable"); //executes Select command and fill the result into tbl variable
//use binding source to bind your controls to the dataset
BindingSource myTableBindingSource = new BindingSource();
myTableBindingSource.DataSource = ds;
然後,就這麼簡單,你可以在綁定源使用AddNew()
方法來添加新的記錄,然後用更新的方法保存
adapter.Update(ds, "myTable");
使用此命令來刪除一條記錄:
的適配器,最好的辦法是增加一個DataSet
從Project->Add New Item
菜單,然後按照嚮導...
相關問題
- 1. SQL Server和Oracle連接教程
- 2. 遠程連接到SQL Server
- 3. 連接到遠程SQL Server
- 4. SQL Server/C#連接
- 5. 連接到SQL Server
- 6. 連接到SQL Server
- 7. C#持久連接到SQL Server
- 8. 從C#連接到SQL Server 2008#
- 9. C++ ODBC SQL Server 2008連接
- 10. C#和SQL Server連接
- 11. C#和SQL Server 2005連接
- 12. 與SQL Server的C#連接
- 13. C#SQL Server連接問題
- 14. 關於如何使用vb.net連接/操作sql server的教程
- 15. 無法連接到遠程SQL Server 2005
- 16. 無法連接到SQL Server 2014遠程
- 17. 遠程連接問題到sql server 2008
- 18. 當連接到SQL Server 2005時,SQL Server不允許遠程連接
- 19. Sql server 2000遠程連接
- 20. SQL Server遠程連接
- 21. SQL Server 2005遠程連接
- 22. 從access連接到sql server
- 23. 將form.io連接到SQL Server
- 24. 無法連接到SQL Server
- 25. 連接到SQL Server很慢
- 26. 連接到SQL Server localdb JDBC
- 27. Ruby連接到SQL Server
- 28. 連接到SQL Server(查看)
- 29. 無法連接到SQL Server
- 30. 無法連接到SQL Server
你需要通過GUI來編輯表格,或只是讓你的C#代碼更新呢? – 2010-09-28 17:38:00
@steve我想在gui中編輯表格 – 2010-09-28 17:40:07
爲什麼C#是一個需求呢?你不能只使用SQL Server Management Studio嗎? (可通過SQL Server Express免費獲得) – clifgriffin 2010-09-28 17:41:12