如何在c#中从sql server运行代码 [英] How to run code from sql server in c#

查看:30
本文介绍了如何在c#中从sql server运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 SqlCommand cmd = new SqlCommand("select Developer from Stackoverflow 
 where UserID='786' and Developer='Sufiyan'", con);
 if (con.State == ConnectionState.Closed)
 {
    con.Open();
 }
 SqlDataReader reader = cmd.ExecuteReader();
 if (reader.Read())
 {
    string codeSnippet = reader["CodeSnippet"].ToString();
    dynamic script = 
     CSScript.LoadCode(@"
     using System;
     using System.Windows.Forms;
     using System.Collections.Generic;
     using System.Data;
     using System.Windows.Forms;
     using System.Data.SqlClient;
     public class RunFromSqlServer
     {
       public void RunCode(string developer){" + codeSnippet + "}"+
               "}").CreateObject("*");script.RunCode("Mohammad Sufiyan Shaikh");
 }
 if (con.State == ConnectionState.Open)
 {
    con.Close();
 }

其中 codeSnippet 可以是您要运行的任何 c# 代码我已经发布了这个问题的答案以帮助其他人..

where codeSnippet can be any c# code you want to run i have posted this question with answer to help others..

推荐答案

你不能像那样混合和匹配 SQL 和 c# 代码.有一种方法可以通过使用称为 SQL CLR 的功能在 SQL Server 2005+ 上运行 .net 代码,但您可以执行的操作存在限制.这是一个教程:如何:创建和运行 CLR SQL Server 用户定义函数 (原始链接).

You can't really mix and match SQL and c# code like that. There is a way of running .net code on SQL server 2005+ by using a feature called SQL CLR but there are limitations to what you can do. Here is a tutorial: How to: Create and Run a CLR SQL Server User-Defined Function (original link).

您还可以创建其他内容,例如 SQL CLR 触发器和存储过程.研究 SQL CLR 以获取更多信息.

There are other things you can create such as SQL CLR triggers and Stored procedures. Research SQL CLR for more info.

这篇关于如何在c#中从sql server运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆