读取SQLServer数据库存储过程列表及参数信息
得到数据库存储过程列表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name
得到某个存储过程的参数信息:(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]'))
得到某个存储过程的参数信息:(Ado.net方法)
SqlCommandBuilder.DeriveParameters(mysqlcommand);
得到数据库所有表:
select * from dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 order by name
得到某个表中的字段信息:
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. 得到数据库存储过程列表:
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. 得到某个存储过程的参数信息:(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. 列出所有数据库:
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>
/// 列出所有数据库
/// </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 = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
L = 日志
FN = 标量函数
IF = 内嵌表函数
P = 存储过程
PK = PRIMARY KEY 约束(类型是 K)
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
UQ = UNIQUE 约束(类型是 K)
V = 视图
X = 扩展存储过程
*/
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函数列表:
public string[] GetFunctionList()
{
return GetList("SELECT name FROM sysobjects WHERE xtype='FN' AND name <> 'dtproperties' order by name asc");
}
7. 得到存储过程列表:
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,Function,存储过程的参数,Field信息:
public string[] GetTableFields(string tableName)
{
return GetList(String.Format("select name from syscolumns where id =object_id('{0}')", tableName));
}
10. 得到Table各个Field的详细定义:
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. 得到存储过程内容:
类似“8. 得到table的索引Index信息”,SQL语句为:EXEC Sp_HelpText '存储过程名'
12. 得到视图View定义:
类似“8. 得到table的索引Index信息”,SQL语句为:EXEC Sp_HelpText '视图名'
(以上代码可用于代码生成器,列出数据库的所有信息)
http://www.cnblogs.com/luluping/archive/2009/07/24/1530528.html
读取SQLServer数据库存储过程列表及参数信息的更多相关文章
- SqlServer数据库存储过程的使用
搞开发这么久了,说实话很少用到存储过程,大部分代码都在业务层给处理了. 今天在做APP接口的时候,表里面没有数据,需要模拟一些数据,于是就想到存储过程,来一起用下吧. SqlServer中创建存储过程 ...
- 【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)
先前条件:设置好数据库,需要三个文件CommandInfo.cs.DbHelperSQL.cs.myHelper.cs,需要修改命名空间,参照之前随笔http://www.cnblogs.com/Ow ...
- js读取sqlserver数据库,输出至html
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- C# 得到sqlserver 数据库存储过程,触发器,视图,函数 的定义
经常从 生产环境 到测试环境, 需要重新弄一整套的数据库环境, 除了表结构以及表结构数据,可以用动软代码生成器 生成之外, 像 存储过程,触发器,等,好像没有批量操作的,意义哥哥农比较麻烦, 所以最近 ...
- Excel2003读取sqlserver数据库表数据(图)
- JAVA使用JDBC技术操作SqlServer数据库执行存储过程
Java使用JDBC技术操作SqlServer数据库执行存储过程: 1.新建SQLSERVER数据库:java_conn_test 2.新建表:tb_User 3.分别新建三个存储过程: 1>带 ...
- python 读取SQLServer数据插入到MongoDB数据库中
# -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...
- 查询Sqlserver数据库死锁的一个存储过程(转)
使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who ...
- java读取CSV文件添加到sqlserver数据库
在直接将CSV文件导入sqlserver数据库时出现了错误,原因还未找到,初步怀疑是数据中含有特殊字符.于是只能用代码导数据了. java读取CSV文件的代码如下: package experimen ...
随机推荐
- Hadoop: failed on connection exception: java.net.ConnectException: Connection refuse
ssh那些都已经搞了,跑一个书上的例子出现了Connection Refused异常,如下: 12/04/09 01:00:54 INFO ipc.Client: Retrying connect t ...
- sina微博上看到的关于android界面设计相关的规范
图片来自:http://photo.weibo.com/5174249907/wbphotos/large/mid/3777508610941685/pid/005EaCLFjw1emcpzdgrj9 ...
- 转 Android网络编程之使用HttpClient批量上传文件 MultipartEntityBuilder
请尊重他人的劳动成果,转载请注明出处:Android网络编程之使用HttpClient批量上传文件 http://www.tuicool.com/articles/Y7reYb 我曾在<Andr ...
- jquery.proxy的四种使用场景及疑问
作者:zccst 其实只有两种使用方式,只不过每一种又细分是否传参. 先给一段HTML,后面会用来测试: <p><button id="test">Test ...
- linux usb installer
其实很简单,手册上有,cp debian....iso /dev/sdc,但是要把sdc上的分区删掉了先. This will only work if it is a hybrid ISO cont ...
- Laravel Eloquent get获取空的数据问题
在用laravel框架来获取数据,若数据不存在时,以为会返回空,其实不是的,其实是一个 collection 值,会返回如下: object(Illuminate\Database\Eloquent\ ...
- Leetcode题1
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- hdu 2614
#include<stdio.h> int map[99][99]; int vist[99]; int sum=1; int maxsum=1; int max=0; int N; vo ...
- C#调用bat 不显示DOS窗口,禁止DOS窗口一闪而过
ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true;//不创建窗口
- 批量检查APK是否具有指定的权限。
为测试组的妹子提供的. 效果如下: 目录结构如下: 源代码思路: 1.将apk文件变为zip文件.这里是修改后缀 2.解压文件到指定目录.可以只解压其中mainfest.xml文件 3.移动xml文件 ...