C#如何用程序实现将Store procedure(存储过程)直接写入SQL数据库,散分了~~~~~~~

heiguangbao 2003-11-14 04:36:28
向各位请教一个小小的问题:

在C#程序中如何将一段Store Procedure直接写入SQL Server数据库中,
也就是说,我只要在窗体中写好存储过程,点击按钮就可以自动见个存储过程写入
指定的数据库,就相当于SQL中查询分析器一样。

我相信各位高手对这种小问题一定是不屑一顾,那我就希望各位高手们帮帮忙了

就当小弟散分了~~~~
...全文
115 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Edifier0709 2003-11-14
  • 打赏
  • 举报
回复
up
HanYZ 2003-11-14
  • 打赏
  • 举报
回复
已验证,完全可以,代码如下,点两次的话就会提示存储过程已经存在了

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();
}
}
SolidGL 2003-11-14
  • 打赏
  • 举报
回复
好像不能直接用吧,我这样改了一行代码,请各路高手看看。。。。。问题在哪里。



/// ************************************************************************
/// * 项目名称:
/// * 项目介绍:
/// *
/// ************************************************************************
/// * 模块名称:
/// * 模块功能:
/// *
/// *
/// *
/// ************************************************************************
/// * 模块:
/// * 创建人:
/// * 创建时间:
/// *
/// * 变更记录:
/// * 第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="";
}
}
}
heiguangbao 2003-11-14
  • 打赏
  • 举报
回复
可是我运行后没有结果呀,这是怎么回事??

谁能写个完整的代码出来。
ivt 2003-11-14
  • 打赏
  • 举报
回复
string sql="exec p_xx param1,param2,.....,out paramn";
就是一条sql语句呀
巍巍清风 2003-11-14
  • 打赏
  • 举报
回复
同意 brightheroes(太菜了,请原谅) , 如果存在,先删去,再新建。

用一个SqlCommand来执行这个 SQL 语句。
youngby 2003-11-14
  • 打赏
  • 举报
回复
哦!?
commanttext=sqltext
......
.......
.....
调用executeReader方法就可以了。
jackyhzzjcn 2003-11-14
  • 打赏
  • 举报
回复
直接执行CREATE PROCEDURE SQL语句,和SELECT一样用法
速马 2003-11-14
  • 打赏
  • 举报
回复
和一个普通的command一样, 不过你至少需要有建立存储过程的权限
brightheroes 2003-11-14
  • 打赏
  • 举报
回复
类似于
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]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

create 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
速马 2003-11-14
  • 打赏
  • 举报
回复
myConnection.Open();

SqlCommand CreateProcCommand = new SqlCommand("CREATE PROCEDURE ....);

CreateProcCommand.ExecuteNonQuery();
myConnection.Close();
brightheroes 2003-11-14
  • 打赏
  • 举报
回复
其实你可以用一个sql语句来实现
create procedure.....
然后直接执行这个sql语句就可以了

brightheroes 2003-11-14
  • 打赏
  • 举报
回复
如果不用sqlDMO.DLL的话,就直接调用osql.exe

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧