using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;public partial class StoredProcedures{ [Microsoft.SqlServer.Server.SqlProcedure] public static void StoredProcedure1(string name) { SqlPipe sp = SqlContext.Pipe; string sql = "insert into idcode(idcode)values('"+name+"')"; using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.Connection = conn; cmd.CommandText = sql; SqlDataReader rdr = cmd.ExecuteReader(); sp.Send(rdr); conn.Close(); } }};
Note: this program does not use the mydbase class, but uses "context. Connection = true" (for details, see the context. Connection help documentation) to connect to the database.
Google Translate: Internal data access problems are quite common scenarios. That is to say, you want to access the servers that use the same Common Language Runtime (CLR) stored procedures or functions. One option is to create a connection using system. Data. sqlclient. sqlconnection, specify the connection string pointing to the local server, and open the connection. You need to specify the creden to log on. The connection is in a different database session than the stored procedure or function. It may have different settings. It is in a separate transaction, it does not look at your temporary table, and so on. If your stored procedure or function code is executed in the SQL server process, this is because someone connects to the server and executes an SQL statement to call it. You may want to execute a stored procedure or function in this context, along with its transactions, set options, and so on. This is the so-called context connection.
CLR Stored Procedure