Wednesday, November 30, 2011

Connection C# and SQL Server 2005

Programming in C# .NET in ms visual studio net 2003 used SQL server 2005 connection :
1) Create database hwsw in sql server 2005 with table inventory and field is : idinventory, nameinventory, transaction inventory. etc
2) Create Form in C# net and drag datagrid in toolbox, with source code below:
             string connectionString = "Data Source=172.16.10.14;Initial Catalog=hwsw;Integrated   Security=False; User id=xxx;Password=xxxx";
            string sql = "SELECT * FROM inventory";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();           
            sCommand = new SqlCommand(sql, connection);
            sAdapter = new SqlDataAdapter(sCommand);
            sBuilder = new SqlCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "inventory");
            sTable = sDs.Tables["inventory"];
            connection.Close();
            dataGrid1.DataSource = sDs.Tables["inventory"];
            dataGrid1.ReadOnly = true;
            save_btn.Enabled = false;