调用CLR存储过程(Invoking CLR stored procedures)

2019-09-02 03:49发布

总之,在哪里可以找到C#/ VB客户端的示例代码调用CLR存储过程与一些argumnet [像SQLXML数据],并接收一个DataReader或结果的其他形式?

还有我怎么定期收到来自通过SQlContext.Pipe.Send()方法发送的运行CLR存储过程的信息?

Answer 1:

// run a stored procedure that takes a parameter
    public void RunStoredProcParams()
    {
        SqlConnection conn = null;
        SqlDataReader rdr  = null;

        // typically obtained from user
        // input, but we take a short cut
        string custId = "FURIB";

        Console.WriteLine("\nCustomer Order History:\n");

        try
        {
            // create and open a connection object
            conn = new 
                SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
            conn.Open();

            // 1.  create a command object identifying
            //     the stored procedure
            SqlCommand cmd  = new SqlCommand(
                "dbo.CustOrderHist", conn);

            // 2. set the command object so it knows
            //    to execute a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which
            //    will be passed to the stored procedure
            cmd.Parameters.Add(
                new SqlParameter("@CustomerID", custId));

            // execute the command
            rdr = cmd.ExecuteReader();

            // iterate through results, printing each to console
            while (rdr.Read())
            {
                Console.WriteLine(
                    "Product: {0,-35} Total: {1,2}",
                    rdr["ProductName"],
                    rdr["Total"]);
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
        }   
    }
}

    enter code here


Answer 2:

string connectionString = ConfigurationManager.AppSettings["ConnectDB"];
        SqlConnection sn = new SqlConnection(connectionString);
        SqlParameter[] sqlParameters = new SqlParameter[1];
        sn.Open();
        SqlCommand dCmd = new SqlCommand("dbo.HelloWorld", sn);
        dCmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader rdr = null;
        rdr = dCmd.ExecuteReader();
        while (rdr.Read())
            {
            for (int i = 0; i < rdr.FieldCount; i++)
                Response.Write(rdr[i]);
            }
        sn.Close();
        }


Answer 3:

用同样的方法调用普通stroed程序。 也许...

EXEC StoredProcedure1

我写了一篇博客长回来- 写你的第一个SQL Server的CLR存储过程



Answer 4:

我知道,我通常创建一个普通的T-SQL存储过程,它调用我的CLR函数或存储的特效。 然后,他们可以治疗的,就像所有其他存储的特效。



Answer 5:

  // Create a record object that represents an individual row, including it's metadata.
      SqlDataRecord record = new SqlDataRecord(new SqlMetaData("stringcol", SqlDbType.NVarChar, 128));

      // Populate the record.
      record.SetSqlString(0,( "Hello World!" + System.DateTime.Now));

      // Send the record to the client.
      SqlContext.Pipe.Send(record);


文章来源: Invoking CLR stored procedures