CLR Stored Procedure

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.