我正在使用C#编写数据库应用程序.我需要在表格上显示数据,现在我已经完成了.但我的业务逻辑硬编码到我的代码.现在我想继续使用我的代码存储过程.我需要做哪些修改.一个简单的步骤列表就够了:)
SqlConnection myConnection = new SqlConnection("user id=dbblabla;" + "password=1234;server=localhost\\SQLEXPRESS;" + "Trusted_Connection=yes;" + "database=myDB; " + "connection timeout=30"); try { myConnection.Open(); } catch (SqlException excep){ Console.WriteLine(excep.StackTrace); } String selectionQuery = "SELECT * FROM myTable"; SqlDataAdapter myAdapter = new SqlDataAdapter(selectionQuery,myConnection); DataSet ds = new DataSet(); myAdapter.Fill(ds,"AllInfo"); dataGridSearchOutput.DataSource = ds.Tables["AllInfo"].DefaultView;
我从创建一个新的SQL命令开始,但我不确定我使用的是正确的方法.
SqlCommand newCommand = new SqlCommand("SELECT * FROM PMInfo"); newCommand.CommandType = CommandType.StoredProcedure;
adatapost.. 6
存储过程
CREATE PROCEDURE addemp @eno int, @ename varchar(50), @edate datetime AS declare @p int select @p=count(*) from emp where eno=@eno if @p=0 begin insert into emp values (@eno,@ename,@edate) end RETURN
C#代码
SqlConnection cn = new SqlConnection(@"conn_str"); SqlCommand cmd = new SqlCommand("addemp", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@eno", 10); cmd.Parameters.AddWithValue("@ename", "Mr.Xyz"); cmd.Parameters.AddWithValue("@edate", DateTime.Parse("1-1-2002")); cn.Open(); cmd.ExecuteNonQuery(); cn.Close();
anishMarokey.. 5
存储过程
CREATE PROCEDURE procedure AS BEGIN SET NOCOUNT ON; SELECT field1,field2 from tbl END GO
码
using(SqlConnection con = new SqlConnection("user id=dbblabla;password=1234;server=localhost\\SQLEXPRESS; database=myDB; connection timeout=30")) { SqlCommand cmd = new SqlCommand("procedure",con); cmd.CommandType= System.Data.CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); dataGridSearchOutput.DataSource = dt; }
我使用的原因是检查此链接
存储过程
CREATE PROCEDURE addemp @eno int, @ename varchar(50), @edate datetime AS declare @p int select @p=count(*) from emp where eno=@eno if @p=0 begin insert into emp values (@eno,@ename,@edate) end RETURN
C#代码
SqlConnection cn = new SqlConnection(@"conn_str"); SqlCommand cmd = new SqlCommand("addemp", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@eno", 10); cmd.Parameters.AddWithValue("@ename", "Mr.Xyz"); cmd.Parameters.AddWithValue("@edate", DateTime.Parse("1-1-2002")); cn.Open(); cmd.ExecuteNonQuery(); cn.Close();
存储过程
CREATE PROCEDURE procedure AS BEGIN SET NOCOUNT ON; SELECT field1,field2 from tbl END GO
码
using(SqlConnection con = new SqlConnection("user id=dbblabla;password=1234;server=localhost\\SQLEXPRESS; database=myDB; connection timeout=30")) { SqlCommand cmd = new SqlCommand("procedure",con); cmd.CommandType= System.Data.CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); dataGridSearchOutput.DataSource = dt; }
我使用的原因是检查此链接