我已经在sql中编写了存储过程并成功执行了存储过程搜索数据库中输入的字符串并将数据插入名为tempdb.dbo.result的表中.
这是我在数据库中搜索字符串的存储过程:
Use tempdbGOCreate table Result( [Sno] int IDentity(1,1),[Database name] sysname,[Schema name] sysname,[table name] sysname,[Column name] sysname,[Record name] varchar(Max))USE TestDB2GOCREATE PROCEDURE Find_Record_Across_tables_Proc @Database sysname,@Schema sysname,@table sysname,@String VARCHAR(Max)AS DECLARE @sqlString varchar(Max) DECLARE @table_Schema sysname DECLARE @table_name sysname DECLARE @Column_name sysname --Declare Cursor SET @sqlString = 'DECLARE String_cursor CURSOR FOR Select table_SCHEMA,table_name,ColUMN_name from ' + @Database +'.informatION_SCHEMA.ColUMNS Where DATA_TYPE IN (''text'',''ntext'',''varchar'',''nvarchar'',''char'',''nchar'')' --Filter schema name IF @schema IS NOT NulL Begin SET @sqlString = @sqlString + ' And table_SCHEMA=''' + @Schema + '''' End --Filter table name IF @table IS NOT NulL Begin SET @sqlString = @sqlString + ' And table_name=''' + @table + '''' End Print @sqlString EXEC (@sqlString)OPEN String_cursorFETCH NEXT FROM String_cursorINTO @table_Schema,@table_name,@Column_nameWHILE @@FETCH_STATUS = 0BEGINSET @sqlString = 'IF EXISTS(SELECT ' + QUOTEname(@Column_name)+ ' FROM ' + @Database + '.' + QUOTEname(@table_Schema)+ '.' + QUOTEname(@table_name)+ ' WHERE ' + QUOTEname(@Column_name)+ ' like ''%' + @string + '%'')Insert into tempdb.dbo.result([Database name],[Schema name],[table name],[Column name],[Record name])SELECT ''' + QUOTEname(@Database) + ''','''+ QUOTEname(@table_Schema) + ''','''+ QUOTEname(@table_name) + ''',''''+ ''' + QUOTEname(@Column_name)+ ''',' + QUOTEname(@Column_name)+ ' FROM ' + @Database + '.'+ QUOTEname(@table_Schema)+ '.' + QUOTEname(@table_name)+ ' WHERE ' + QUOTEname(@Column_name)+ ' like ''%' + @string + '%'''Print @sqlStringEXEC (@sqlString)FETCH NEXT FROM String_cursorINTO @table_Schema,@Column_nameENDCLOSE String_cursorDEALLOCATE String_cursorGO
我通过以下命令在sql中成功执行了这个存储过程:
Use TestDB2GOEXEC Find_Record_Across_tables_Proc'TestDB2(My database name)',NulL,'string to be searched'GOSelect * from tempdb.dbo.resultGO
现在,每当我从Web应用程序执行(调用)此参数化存储过程时,编译器在调用存储过程时都不会显示异常,但它只会在BindGrID()中运行select查询.
这是我的代码:
public partial class WebForm1 : System.Web.UI.Page{ DataSet ds = new DataSet(); sqlConnection con; protected voID Page_Load(object sender,EventArgs e) { } protected voID button1_Click(object sender,EventArgs e) { String value = TextBox1.Text.ToString(); con = new sqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString); con.open(); sqlCommand cmd = new sqlCommand("Find_Record_Across_tables_Proc",con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AdDWithValue("@Database","TestDB2"); cmd.Parameters.AdDWithValue("@Schema","NulL"); cmd.Parameters.AdDWithValue("@table","NulL"); cmd.Parameters.AdDWithValue("@String",value); cmd.ExecuteNonquery(); con.Close(); this.BindGrID(); } private voID BindGrID() { string constr = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString; using (sqlConnection con = new sqlConnection(constr)) { using (sqlCommand cmd = new sqlCommand("Select * from tempdb.dbo.result")) { using (sqlDataAdapter sda = new sqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (Datatable dt = new Datatable()) { sda.Fill(dt); GrIDVIEw1.DataSource = dt; GrIDVIEw1.DataBind(); } } } } } }解决方法 在你的button_Click中为cmd.ExecuteNonquery();只需指定一个int值i
例如:
int i =cmd.ExecuteNonquery(); if(i>0) { this.BindGrID(); }
更新注意:我看到你将DB和Schema作为参数传递给程序,但在连接字符串中你将定义它,当你从sql做到但在进入应用程序时你不需要使用他们因为你将说明web-config哪个数据库和架构,所以它将是一个多余的.
总结以上是内存溢出为你收集整理的如何在C#(ASP.Net)中调用参数化存储过程?全部内容,希望文章能够帮你解决如何在C#(ASP.Net)中调用参数化存储过程?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)