简体   繁体   English

c#连接SQL服务器教程

[英]Tutorial on connecting c# to SQL server

I want to be able to edit a table in a SQL server database using c#.我希望能够使用 c# 编辑 SQL 服务器数据库中的表。

Can someone please show me a very simple tutorial on connecting to the DB and editing data in a table.有人可以给我看一个关于连接到数据库和编辑表中数据的非常简单的教程。

Thank you so much.非常感谢。

First step is to create a connection.第一步是创建连接。 connection needs a connection string. connection 需要一个连接字符串。 you can create your connection strings with a SqlConnectionStringBuilder .您可以使用SqlConnectionStringBuilder创建连接字符串。


SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
connBuilder.InitialCatalog = "DatabaseName";
connBuilder.DataSource = "ServerName";
connBuilder.IntegratedSecurity = true;

Then use that connection string to create your connection like so:然后使用该连接字符串创建您的连接,如下所示:


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;

Then, so simple you can use AddNew() method in the binding source to Add new record and then save it with update method of your adapter:然后,很简单,您可以在绑定源中使用AddNew()方法添加新记录,然后使用适配器的更新方法保存它:

adapter.Update(ds, "myTable");

Use this command to delete a record:使用此命令删除记录:

myTableBindingSource.RemoveCurrent();
adapter.Update(ds, "myTable");

The best way is to add a DataSet from Project->Add New Item menu and follow the wizard...最好的方法是从Project->Add New Item菜单中添加一个DataSet并按照向导...

Assuming you're using Visual Studio as your IDE you could just use LINQ to SQL.假设您使用 Visual Studio 作为您的 IDE,您可以只使用 LINQ to SQL。 It's a pretty simple way to interact with your database and it should be pretty quick to get going.这是一种与数据库交互的非常简单的方法,应该很快上手。

Using LINQ to SQL is a pretty simple walk through in getting it up and running. 使用 LINQ to SQL是启动和运行它的一个非常简单的步骤。

Have a read of the MSDN tutorial on Creating Data Applications .阅读有关创建数据应用程序MSDN 教程 You may be able to clarify your question, or find the answers you need.您或许能够澄清您的问题,或找到您需要的答案。

There is info on editing the data in the app but you have to get connected and load it into your app first.有关于在应用程序中编辑数据的信息,但您必须先建立连接并将其加载到您的应用程序中。

The only reason to do this in C# is if you want to automate it somehow or create an interface for non-technical users to interact with the database.在 C# 中这样做的唯一原因是,如果您想以某种方式自动化它或为非技术用户创建一个界面来与数据库进行交互。 You can use a GridView control with an SQL datasource to manipulate the data.您可以使用带有 SQL 数据源的 GridView 控件来操作数据。

@kevin: if he's just learning, I think its probably simpler to have him use SQLCommand object (or SQLDataAdapter). @kevin:如果他只是在学习,我认为让他使用 SQLCommand 对象(或 SQLDataAdapter)可能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM