C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲(chǔ)過程列表及參數(shù)信息示例
本文實(shí)例講述了C# Ado.net讀取SQLServer數(shù)據(jù)庫存儲(chǔ)過程列表及參數(shù)信息的方法。分享給大家供大家參考,具體如下:
得到數(shù)據(jù)庫存儲(chǔ)過程列表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name
得到某個(gè)存儲(chǔ)過程的參數(shù)信息:(SQL方法)
select * from syscolumns where ID in (SELECT id FROM sysobjects as a WHERE OBJECTPROPERTY(id, N'IsProcedure') = 1 and id = object_id(N'[dbo].[mystoredprocedurename]'))
得到某個(gè)存儲(chǔ)過程的參數(shù)信息:(Ado.net方法)
SqlCommandBuilder.DeriveParameters(mysqlcommand);
得到數(shù)據(jù)庫所有表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 order by name
得到某個(gè)表中的字段信息:
select c.name as ColumnName, c.colorder as ColumnOrder, c.xtype as DataType, typ.name as DataTypeName, c.Length, c.isnullable from dbo.syscolumns c inner join dbo.sysobjects t on c.id = t.id inner join dbo.systypes typ on typ.xtype = c.xtype where OBJECTPROPERTY(t.id, N'IsUserTable') = 1 and t.name='mytable' order by c.colorder;
C# Ado.net代碼示例:
1. 得到數(shù)據(jù)庫存儲(chǔ)過程列表:
using System.Data.SqlClient;
private void GetStoredProceduresList()
{
string sql = "select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name";
string connStr = @"Data Source=(local);Initial Catalog=mydatabase; Integrated Security=True; Connection Timeout=1;";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
using (SqlDataReader MyReader = cmd.ExecuteReader())
{
while (MyReader.Read())
{
//Get stored procedure name
this.listBox1.Items.Add(MyReader[0].ToString());
}
}
}
finally
{
conn.Close();
}
}
2. 得到某個(gè)存儲(chǔ)過程的參數(shù)信息:(Ado.net方法)
using System.Data.SqlClient;
private void GetArguments()
{
string connStr = @"Data Source=(local);Initial Catalog=mydatabase; Integrated Security=True; Connection Timeout=1;";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "mystoredprocedurename";
cmd.CommandType = CommandType.StoredProcedure;
try
{
conn.Open();
SqlCommandBuilder.DeriveParameters(cmd);
foreach (SqlParameter var in cmd.Parameters)
{
if (cmd.Parameters.IndexOf(var) == 0) continue;//Skip return value
MessageBox.Show((String.Format("Param: {0}{1}Type: {2}{1}Direction: {3}",
var.ParameterName,
Environment.NewLine,
var.SqlDbType.ToString(),
var.Direction.ToString())));
}
}
finally
{
conn.Close();
}
}
3. 列出所有數(shù)據(jù)庫:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
private static string connString =
"Persist Security Info=True;timeout=5;Data Source=192.168.1.8;User ID=sa;Password=password";
/// <summary>
/// 列出所有數(shù)據(jù)庫
/// </summary>
/// <returns></returns>
public string[] GetDatabases()
{
return GetList("SELECT name FROM sysdatabases order by name asc");
}
private string[] GetList(string sql)
{
if (String.IsNullOrEmpty(connString)) return null;
string connStr = connString;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
List<string> ret = new List<string>();
using (SqlDataReader MyReader = cmd.ExecuteReader())
{
while (MyReader.Read())
{
ret.Add(MyReader[0].ToString());
}
}
if (ret.Count > 0) return ret.ToArray();
return null;
}
finally
{
conn.Close();
}
}
4. 得到Table表格列表:
private static string connString =
"Persist Security Info=True;timeout=5;Data Source=192.168.1.8;Initial Catalog=myDb;User ID=sa;Password=password";
/* select name from sysobjects where xtype='u' ---
C = CHECK 約束
D = 默認(rèn)值或 DEFAULT 約束
F = FOREIGN KEY 約束
L = 日志
FN = 標(biāo)量函數(shù)
IF = 內(nèi)嵌表函數(shù)
P = 存儲(chǔ)過程
PK = PRIMARY KEY 約束(類型是 K)
RF = 復(fù)制篩選存儲(chǔ)過程
S = 系統(tǒng)表
TF = 表函數(shù)
TR = 觸發(fā)器
U = 用戶表
UQ = UNIQUE 約束(類型是 K)
V = 視圖
X = 擴(kuò)展存儲(chǔ)過程
*/
public string[] GetTableList()
{
return GetList("SELECT name FROM sysobjects WHERE xtype='U' AND name <> 'dtproperties' order by name asc");
}
5. 得到View視圖列表:
public string[] GetViewList()
{
return GetList("SELECT name FROM sysobjects WHERE xtype='V' AND name <> 'dtproperties' order by name asc");
}
6. 得到Function函數(shù)列表:
public string[] GetFunctionList()
{
return GetList("SELECT name FROM sysobjects WHERE xtype='FN' AND name <> 'dtproperties' order by name asc");
}
7. 得到存儲(chǔ)過程列表:
public string[] GetStoredProceduresList()
{
return GetList("select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name asc");
}
8. 得到table的索引Index信息:
public TreeNode[] GetTableIndex(string tableName)
{
if (String.IsNullOrEmpty(connString)) return null;
List<TreeNode> nodes = new List<TreeNode>();
string connStr = connString;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(String.Format("exec sp_helpindex {0}", tableName), conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
using (SqlDataReader MyReader = cmd.ExecuteReader())
{
while (MyReader.Read())
{
TreeNode node = new TreeNode(MyReader[0].ToString(), 2, 2);/*Index name*/
node.ToolTipText = String.Format("{0}{1}{2}", MyReader[2].ToString()/*index keys*/, Environment.NewLine,
MyReader[1].ToString()/*Description*/);
nodes.Add(node);
}
}
}
finally
{
conn.Close();
}
if(nodes.Count>0) return nodes.ToArray ();
return null;
}
9. 得到Table,View,F(xiàn)unction,存儲(chǔ)過程的參數(shù),F(xiàn)ield信息:
public string[] GetTableFields(string tableName)
{
return GetList(String.Format("select name from syscolumns where id =object_id('{0}')", tableName));
}
10. 得到Table各個(gè)Field的詳細(xì)定義:
public TreeNode[] GetTableFieldsDefinition(string TableName)
{
if (String.IsNullOrEmpty(connString)) return null;
string connStr = connString;
List<TreeNode> nodes = new List<TreeNode>();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(String.Format("select a.name,b.name,a.length,a.isnullable from syscolumns a,systypes b,sysobjects d where a.xtype=b.xusertype and a.id=d.id and d.xtype='U' and a.id =object_id('{0}')",
TableName), conn);
cmd.CommandType = CommandType.Text;
try
{
conn.Open();
using (SqlDataReader MyReader = cmd.ExecuteReader())
{
while (MyReader.Read())
{
TreeNode node = new TreeNode(MyReader[0].ToString(), 2, 2);
node.ToolTipText = String.Format("Type: {0}{1}Length: {2}{1}Nullable: {3}", MyReader[1].ToString()/*type*/, Environment.NewLine,
MyReader[2].ToString()/*length*/, Convert.ToBoolean(MyReader[3]));
nodes.Add(node);
}
}
if (nodes.Count > 0) return nodes.ToArray();
return null;
}
finally
{
conn.Close();
}
}
11. 得到存儲(chǔ)過程內(nèi)容:
類似“8. 得到table的索引Index信息”,SQL語句為:EXEC Sp_HelpText '存儲(chǔ)過程名'
12. 得到視圖View定義:
類似“8. 得到table的索引Index信息”,SQL語句為:EXEC Sp_HelpText '視圖名'
(以上代碼可用于代碼生成器,列出數(shù)據(jù)庫的所有信息)
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見數(shù)據(jù)庫操作技巧匯總》、《C#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- .net core EF Core調(diào)用存儲(chǔ)過程的方式
- .net core2.0下使用Identity改用dapper存儲(chǔ)數(shù)據(jù)(實(shí)例講解)
- ASP.NET MVC用存儲(chǔ)過程批量添加修改數(shù)據(jù)操作
- asp.net中調(diào)用存儲(chǔ)過程的方法
- asp.net中調(diào)用oracle存儲(chǔ)過程的方法
- VB.NET調(diào)用MySQL存儲(chǔ)過程并獲得返回值的方法
- .Net下二進(jìn)制形式的文件(圖片)的存儲(chǔ)與讀取詳細(xì)解析
- .NET中的字符串在內(nèi)存中的存儲(chǔ)方式
相關(guān)文章
C#實(shí)現(xiàn)自定義單選和復(fù)選按鈕樣式
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)定義單選和復(fù)選按鈕樣式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
在.net應(yīng)用程序中運(yùn)行其它EXE文件的方法
這篇文章主要介紹了在.net應(yīng)用程序中運(yùn)行其它EXE文件的方法,涉及C#進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#畫筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法
這篇文章主要介紹了C#畫筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法,實(shí)例分析了畫筆的保存save及恢復(fù)屬性Restore的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
c#生成excel示例sql數(shù)據(jù)庫導(dǎo)出excel
這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導(dǎo)出數(shù)據(jù)到excel,大家參考使用吧2014-01-01
C#利用Windows自帶gdi32.dll實(shí)現(xiàn)抓取屏幕功能實(shí)例
這篇文章主要介紹了C#利用Windows自帶gdi32.dll實(shí)現(xiàn)抓取屏幕功能,是C#程序設(shè)計(jì)中常見的一個(gè)重要技巧,需要的朋友可以參考下2014-08-08

