C#如何用程序实现将Store procedure(存储过程)直接写入SQL数据库,散分了~~~~~~~ 如果不用sqlDMO.DLL的话,就直接调用osql.exe 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实你可以用一个sql语句来实现create procedure.....然后直接执行这个sql语句就可以了 myConnection.Open();SqlCommand CreateProcCommand = new SqlCommand("CREATE PROCEDURE ....);CreateProcCommand.ExecuteNonQuery(); myConnection.Close(); 类似于if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[r_iRTblDBMProps]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[r_iRTblDBMProps]GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOcreate procedure r_iRTblDBMProps (@IntID binary(8),@Z_BranchID_Z int,@Z_VS_Z int,@Col5004 smallint=null,@Col5238 int=null,@Col5254 varchar(255)=null,@Col5203 float=null,@Col5204 float=null,@Col5226 float=null,@Col5227 int=null,@Col5009 int=null,@Col5010 int=null,@Col5011 tinyint=null,@Col5012 tinyint=null,@Col5013 int=null,@Col5007 tinyint=null,@Col5031 tinyint=null,@Col5032 tinyint=null,@Col5033 int=null,@Col5034 tinyint=null,@Col5035 int=null,@Col5239 tinyint=null,@Col5230 text=null,@Col5015 tinyint=null,@Col5017 tinyint=null,@Col5029 tinyint=null,@Col5023 tinyint=null,@Col5024 tinyint=null,@Col5025 tinyint=null,@Col5026 int=null,@Col5027 int=null,@Col5350 int=null,@Col5304 varchar(255)=null,@Col5305 varchar(255)=null,@Col5306 varchar(255)=null,@Col5307 int=null,@Col5308 smallint=null,@Col5309 smallint=null,@Col5351 bit=null,@Col5311 bit=null,@Col5352 bit=null,@Col5353 bit=null,@Col5314 bit=null,@Col5315 bit=null,@Col5354 bit=null,@Col11131 varchar(255)=null,@Col11119 varchar(255)=null,@Col11120 varchar(255)=null,@Col11121 varchar(255)=null,@Col11122 int=null,@Col11123 int=null,@Col11124 tinyint=null,@Col11125 varchar(255)=null,@Col11126 varchar(255)=null,@Col11127 varchar(255)=null,@Col11128 varchar(255)=null,@Col11129 tinyint=null,@Col5262 tinyint=null,@Col5256 smallint=null,@Col5355 varchar(255)=null,@Col5356 varchar(255)=null,@Col5272 varchar(255)=null,@Col5319 int=null,@Col5287 bit=null,@Col5359 int=null,@Col5360 int=null,@Col5361 int=null,@Col5362 int=null,@Col5363 tinyint=null,@Z_VE_Z int=2147483647) as insert RTblDBMProps values (@IntID,@Z_BranchID_Z,@Z_VS_Z,@Z_VE_Z,@Col5004,@Col5238,@Col5254,@Col5203,@Col5204,@Col5226,@Col5227,@Col5009,@Col5010,@Col5011,@Col5012,@Col5013,@Col5007,@Col5031,@Col5032,@Col5033,@Col5034,@Col5035,@Col5239,@Col5230,@Col5015,@Col5017,@Col5029,@Col5023,@Col5024,@Col5025,@Col5026,@Col5027,@Col5350,@Col5304,@Col5305,@Col5306,@Col5307,@Col5308,@Col5309,@Col5351,@Col5311,@Col5352,@Col5353,@Col5314,@Col5315,@Col5354,@Col11131,@Col11119,@Col11120,@Col11121,@Col11122,@Col11123,@Col11124,@Col11125,@Col11126,@Col11127,@Col11128,@Col11129,@Col5262,@Col5256,@Col5355,@Col5356,@Col5272,@Col5319,@Col5287,@Col5359,@Col5360,@Col5361,@Col5362,@Col5363)GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO 和一个普通的command一样, 不过你至少需要有建立存储过程的权限 直接执行CREATE PROCEDURE SQL语句,和SELECT一样用法 哦!?commanttext=sqltext..................调用executeReader方法就可以了。 同意 brightheroes(太菜了,请原谅) , 如果存在,先删去,再新建。用一个SqlCommand来执行这个 SQL 语句。 string sql="exec p_xx param1,param2,.....,out paramn";就是一条sql语句呀 好像不能直接用吧,我这样改了一行代码,请各路高手看看。。问题在哪里。/// ************************************************************************/// * 项目名称:/// * 项目介绍:/// * /// ************************************************************************/// * 模块名称:/// * 模块功能:/// * /// */// */// ************************************************************************/// * 模块:/// * 创建人:/// * 创建时间:/// */// * 变更记录:/// * 第X次变更:/// * 变更原因:/// * 修改人:/// * 代码变化:/// */// ************************************************************************using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient ;namespace WindowsApplication15{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private const string constr="data source=APJ006;initial catalog=Northwind;integrated security=SSPI;persist security info=True;workstation id=APJ006;packet size=4096"; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(32, 24); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(376, 160); this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; // // button1 // this.button1.Location = new System.Drawing.Point(320, 224); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(448, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.textBox1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { System.Data.SqlClient.SqlConnection sc =new SqlConnection(constr); System.Data.SqlClient.SqlCommand scom=new SqlCommand(textBox1.Text,sc); // scom.CommandText=textBox1.Text ; // scom.CommandType=CommandType.Text ; sc.Open (); try { scom.ExecuteNonQuery(); } catch { } finally { sc.Close(); } } private void Form1_Load(object sender, System.EventArgs e) { this.textBox1.Text=""; } }} 已验证,完全可以,代码如下,点两次的话就会提示存储过程已经存在了private void button6_Click(object sender, System.EventArgs e){ try { sqlConnection1.Open(); sqlCommand1.CommandText = "CREATE PROC xxxx AS SELECT TOP 3 * FROM dbo.syscolumns"; sqlCommand1.ExecuteNonQuery(); } catch(System.Exception ee) { MessageBox.Show(ee.Message ); } finally { sqlConnection1.Close(); }} winform打包成exe的问题 winform 中连接access数据库,用insert语句向表中添加数据代码如何实现? 请演示一下IComparable 接口和 IComparer接口用法。不太懂。 GDI+通过键盘上的上、下、左、右,实现放大、缩小、移动功能 在没有任何数据时进行无效的读取尝试。 正则高手请进----如何用正则匹配txt文档中的内容,并读取出匹配的内容~~~ 关于FindControl的用法请教 c# 和 Excel 的问题 终端短信平台开发新手问题 大虾帮忙!! 存储过程的参数问题 水晶报表字段显示问题,高分求解,在线!!
create procedure.....
然后直接执行这个sql语句就可以了
myConnection.Close();
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[r_iRTblDBMProps]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[r_iRTblDBMProps]
GOSET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GOcreate procedure r_iRTblDBMProps (@IntID binary(8),@Z_BranchID_Z int,@Z_VS_Z int,@Col5004 smallint=null,@Col5238 int=null,@Col5254 varchar(255)=null,@Col5203 float=null,@Col5204 float=null,@Col5226 float=null,@Col5227 int=null,@Col5009 int=null,@Col5010 int=null,@Col5011 tinyint=null,@Col5012 tinyint=null,@Col5013 int=null,@Col5007 tinyint=null,@Col5031 tinyint=null,@Col5032 tinyint=null,@Col5033 int=null,@Col5034 tinyint=null,@Col5035 int=null,@Col5239 tinyint=null,@Col5230 text=null,@Col5015 tinyint=null,@Col5017 tinyint=null,@Col5029 tinyint=null,@Col5023 tinyint=null,@Col5024 tinyint=null,@Col5025 tinyint=null,@Col5026 int=null,@Col5027 int=null,@Col5350 int=null,@Col5304 varchar(255)=null,@Col5305 varchar(255)=null,@Col5306 varchar(255)=null,@Col5307 int=null,@Col5308 smallint=null,@Col5309 smallint=null,@Col5351 bit=null,@Col5311 bit=null,@Col5352 bit=null,@Col5353 bit=null,@Col5314 bit=null,@Col5315 bit=null,@Col5354 bit=null,@Col11131 varchar(255)=null,@Col11119 varchar(255)=null,@Col11120 varchar(255)=null,@Col11121 varchar(255)=null,@Col11122 int=null,@Col11123 int=null,@Col11124 tinyint=null,@Col11125 varchar(255)=null,@Col11126 varchar(255)=null,@Col11127 varchar(255)=null,@Col11128 varchar(255)=null,@Col11129 tinyint=null,@Col5262 tinyint=null,@Col5256 smallint=null,@Col5355 varchar(255)=null,@Col5356 varchar(255)=null,@Col5272 varchar(255)=null,@Col5319 int=null,@Col5287 bit=null,@Col5359 int=null,@Col5360 int=null,@Col5361 int=null,@Col5362 int=null,@Col5363 tinyint=null,@Z_VE_Z int=2147483647) as insert RTblDBMProps values (@IntID,@Z_BranchID_Z,@Z_VS_Z,@Z_VE_Z,@Col5004,@Col5238,@Col5254,@Col5203,@Col5204,@Col5226,@Col5227,@Col5009,@Col5010,@Col5011,@Col5012,@Col5013,@Col5007,@Col5031,@Col5032,@Col5033,@Col5034,@Col5035,@Col5239,@Col5230,@Col5015,@Col5017,@Col5029,@Col5023,@Col5024,@Col5025,@Col5026,@Col5027,@Col5350,@Col5304,@Col5305,@Col5306,@Col5307,@Col5308,@Col5309,@Col5351,@Col5311,@Col5352,@Col5353,@Col5314,@Col5315,@Col5354,@Col11131,@Col11119,@Col11120,@Col11121,@Col11122,@Col11123,@Col11124,@Col11125,@Col11126,@Col11127,@Col11128,@Col11129,@Col5262,@Col5256,@Col5355,@Col5356,@Col5272,@Col5319,@Col5287,@Col5359,@Col5360,@Col5361,@Col5362,@Col5363)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
commanttext=sqltext
......
.......
.....
调用executeReader方法就可以了。
就是一条sql语句呀
/// * 项目名称:
/// * 项目介绍:
/// *
/// ************************************************************************
/// * 模块名称:
/// * 模块功能:
/// *
/// *
/// *
/// ************************************************************************
/// * 模块:
/// * 创建人:
/// * 创建时间:
/// *
/// * 变更记录:
/// * 第X次变更:
/// * 变更原因:
/// * 修改人:
/// * 代码变化:
/// *
/// ************************************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient ;namespace WindowsApplication15
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private const string constr="data source=APJ006;initial catalog=Northwind;integrated security=SSPI;persist security info=True;workstation id=APJ006;packet size=4096"; /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 24);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(376, 160);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(320, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection sc =new SqlConnection(constr);
System.Data.SqlClient.SqlCommand scom=new SqlCommand(textBox1.Text,sc);
// scom.CommandText=textBox1.Text ;
// scom.CommandType=CommandType.Text ;
sc.Open ();
try
{
scom.ExecuteNonQuery();
}
catch
{ }
finally
{
sc.Close();
} } private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text="";
}
}
}
{
try
{
sqlConnection1.Open();
sqlCommand1.CommandText = "CREATE PROC xxxx AS SELECT TOP 3 * FROM dbo.syscolumns";
sqlCommand1.ExecuteNonQuery();
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message );
}
finally
{
sqlConnection1.Close();
}
}