c# - 如何在 C# 中从 sql server 运行代码

标签 c# sql-server winforms

<分区>

 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# 代码 我已经发布了这个问题和答案以帮助其他人..

最佳答案

您不能像那样混合和匹配 SQL 和 C# 代码。有一种方法可以通过使用称为 SQL CLR 的功能在 SQL Server 2005+ 上运行 .net 代码,但是您可以执行的操作有一些限制。这是一个教程:How to: Create and Run a CLR SQL Server User-Defined Function (original link)。

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

关于c# - 如何在 C# 中从 sql server 运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15293859/

相关文章:

c# - Sequence 比 Entity Framework 中的 Identity 快吗?

sql - 参数嗅探

c# - 在 BindingSource 中组合多个列表

c# - 如何计算并行的完成百分比?

sql - with(nolock) 、 (nolock) 、 nolock 区别?

c# - 始终加密 : Failed to decrypt a column encryption key using key store provider: 'MSSQL_CERTIFICATE_STORE'

c# - 尝试获取组合框的值时出现 "Cross-thread operation is not valid"异常

.net - 您如何处理下拉样式控件中的多项选择?

c# - 在 C# 中每 x 秒执行一次操作,持续 y 分钟

C# MySql 连接。如何传递图像?