CREATE PROCEDURE [OWNER].[PROCEDURE NAME] AS
CREATE PROCEDURE [dbo].[GetProducts] AS
CREATE PROCEDURE [dbo].[GetProducts] AS SELECT ProductID, ProductName FROM Products
private void Form1_Load(object sender, System.EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Northwind;Integrated Security=SSPI"); SqlCommand command = new SqlCommand("GetProducts", conn); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); this.dataGrid1.DataSource = ds; this.dataGrid1.DataMember = "Products"; }
CREATE PROCEDURE [dbo].[GetProducts] (@CategoryID int) AS SELECT ProductID, ProductName FROM Products WHERE CategoryID = @CategoryID
CREATE PROCEDURE [dbo].[SomeProcedure] ( @Param1 int, @Param2 varchar(50), @Param3 varchar(50) ) AS ...
SELECT ProductID, ProductName FROM Products WHERE CategoryID = 1
exec GetProducts X
SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Northwind;Integrated Security=SSPI"); SqlCommand command = new SqlCommand("GetProducts", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1; SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); this.dataGrid1.DataSource = ds; this.dataGrid1.DataMember = "Products";
command.Parameters.Add("@CategoryID", SqlDbType.Int); command.Parameters["@CategoryID"].Value = 1;
command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1;
SqlConnection conn = new SqlConnection("Data Source=localhost;Database=Northwind;Integrated Security=SSPI"); SqlCommand command = new SqlCommand("GetProducts", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1; conn.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["ProductName"]); } conn.Close();
CREATE PROCEDURE [dbo].[InsertUser] ( @Username varchar(50), @Password varchar(50) ) AS INSERT INTO Users VALUES(@Username, @Password)
string username = ... // get username from user string password = ... // get password from user SqlConnection conn = new SqlConnection("Data Source=localhost;Database=MyDB;Integrated Security=SSPI"); SqlCommand command = new SqlCommand("InsertUser", conn); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@Username", SqlDbType.VarChar).Value = username; command.Parameters.Add("@Password", SqlDbType.VarChar).Value = password; conn.Open(); int rows = command.ExecuteNonQuery(); conn.Close();
if (rows == 1) { MessageBox.Show("Create new user SUCCESS!"); } else { MessageBox.Show("Create new user FAILED!"); }
DELETE FROM Products WHERE ProductID > 50
Build Your Own ASP.NET Website Using C# & VB.NET