Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to .net.I want to create a table and populate the data into the table using sqlserver.So please provide me some sample easy basic code so that i can try on my own.
Posted

Hi,

Please use the code:

string queryString =
"SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

You could then access the table as;

DataTable dt = customers.Tables[0];

Hope this helps you!

Thanks.
 
Share this answer
 
This example uses a winforms with a TextBox (called myTextBox) and a DataGridView (called myDataGridView)
Try:
C#
>            using (SqlConnection con = new SqlConnection(strConnect))
                {
                con.Open();
                using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
                    {
                    da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    myDataGridView.DataSource = dt;
                    }
                }

You will need to provide the connection string to SQL (strConnect) and tailor the code to your exact requirements.
 
Share this answer
 
Comments
vinod.sambangi 19-Nov-12 4:17am    
string connectionstring = " Data Source=01HW235982\SQLEXPRESS2008;Initial Catalog=adarsh;Persist Security Info=True;User ID=sa;Password=Pass@123";

SqlConnection con = new SqlConnection(connectionstring);

con.Open();
SqlCommand com = new SqlCommand("INSERT INTO EMPLOYEE_TABLE(EmployeeID,Name,Address) VALUES (@EmployeeID, @Name,@Address )", con);

com.Parameters.AddWithValue("@EmployeeID", "122");
com.Parameters.AddWithValue("@Name", "m1");
com.Parameters.AddWithValue("@Address", "mj3");
com.ExecuteNonQuery();
vinod.sambangi 19-Nov-12 4:19am    
i am getting error in Datasource name in the foolowing above code.What should i do?
OriginalGriff 19-Nov-12 4:26am    
Check the 01HW235982\SQLEXPRESS2008 part. The way I do it is:
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.
vinod.sambangi 19-Nov-12 4:46am    
i did as you said,connection was estableshed
.This time i used windows authentication while i was trying to establish a connection.A copied the connection string and pasted in the code.But It was saying error at beside the "\" mark as "unrecognized escape sequence".Please provide some solution.
vinod.sambangi 19-Nov-12 4:50am    
error beside "\" mark in 01HW235982\SQLEXPRESS2008 .It was saying error as unrecognized escape sequence.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900