3

I want to be able to edit a table in a SQL server database using c#.

Can someone please show me a very simple tutorial on connecting to the DB and editing data in a table.

Thank you so much.

7
  • Do you need to edit the table via a GUI, or just make updates to it in your C# code? Commented Sep 28, 2010 at 17:38
  • @steve i would like to edit the table in a gui Commented Sep 28, 2010 at 17:40
  • Why is C# a requirement then? Can't you just use SQL Server Management Studio? (available for free with SQL Server Express) Commented Sep 28, 2010 at 17:41
  • @jenny - @clifgriffin is correct, you can do it that way if thaty's all you wish. Can you tell us more about why you need your own code to do this? You can edit the q to add clarifying remarks. Commented Sep 28, 2010 at 17:43
  • 1
    Re: my comment If C# isn't an actual requirement, please see: microsoft.com/downloads/en/… Commented Sep 28, 2010 at 17:43

4 Answers 4

10

First step is to create a connection. connection needs a connection string. you can create your connection strings with a 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:

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...

Sign up to request clarification or add additional context in comments.

1 Comment

where do you specify password ?
4

Assuming you're using Visual Studio as your IDE you could just use 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.

Comments

3

Have a read of the MSDN tutorial on Creating Data Applications. 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.

Comments

2

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. You can use a GridView control with an SQL datasource to manipulate the data.

@kevin: if he's just learning, I think its probably simpler to have him use SQLCommand object (or SQLDataAdapter).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.