This article describes the method of C# reading the stored procedure list and parameter information of SQLServer database. Share it for your reference, as follows:
Get the database stored procedures list:
select * from where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name
Get the parameter information of a stored procedure: (SQL method)
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]'))
Obtain the parameter information of a stored procedure: (Method)
(mysqlcommand);
Get all the tables in the database:
select * from where OBJECTPROPERTY(id, N'IsUserTable') = 1 order by name
Get field information in a table:
select as ColumnName, as ColumnOrder, as DataType, as DataTypeName, , from c inner join t on = inner join typ on = where OBJECTPROPERTY(, N'IsUserTable') = 1 and ='mytable' order by ;
C# code example:
1. Get the database stored procedures list:
using ; private void GetStoredProceduresList() { string sql = "select * from 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); = ; try { (); using (SqlDataReader MyReader = ()) { while (()) { //Get stored procedure name this.(MyReader[0].ToString()); } } } finally { (); } }
2. Get the parameter information of a stored procedure: (Method)
using ; 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(); = conn; = "mystoredprocedurename"; = ; try { (); (cmd); foreach (SqlParameter var in ) { if ((var) == 0) continue;//Skip return value ((("Param: {0}{1}Type: {2}{1}Direction: {3}", , , (), ()))); } } finally { (); } }
3. List all databases:
using System; using ; using ; using ; using ; using ; private static string connString = "Persist Security Info=True;timeout=5;Data Source=192.168.1.8;User ID=sa;Password=password"; /// <summary> /// List all databases/// </summary> /// <returns></returns> public string[] GetDatabases() { return GetList("SELECT name FROM sysdatabases order by name asc"); } private string[] GetList(string sql) { if ((connString)) return null; string connStr = connString; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(sql, conn); = ; try { (); List<string> ret = new List<string>(); using (SqlDataReader MyReader = ()) { while (()) { (MyReader[0].ToString()); } } if ( > 0) return (); return null; } finally { (); } }
4. Get the Table Table List:
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 constraint D = Default value or DEFAULT constraint F = FOREIGN KEY Constraint L = log FN = scalar function IF = Inline Table Function P = Stored Procedure PK = PRIMARY KEY constraint (type is K) RF = Copy filtering stored procedure S = System Table TF = table function TR = Trigger U = User Table UQ = UNIQUE constraint (type is K) V = View X = Extended stored procedure */ public string[] GetTableList() { return GetList("SELECT name FROM sysobjects WHERE xtype='U' AND name <> 'dtproperties' order by name asc"); }
5. Get the View View List:
public string[] GetViewList() { return GetList("SELECT name FROM sysobjects WHERE xtype='V' AND name <> 'dtproperties' order by name asc"); }
6. Get the function list:
public string[] GetFunctionList() { return GetList("SELECT name FROM sysobjects WHERE xtype='FN' AND name <> 'dtproperties' order by name asc"); }
7. Get the stored procedure list:
public string[] GetStoredProceduresList() { return GetList("select * from where OBJECTPROPERTY(id, N'IsProcedure') = 1 order by name asc"); }
8. Get the index index information of the table:
public TreeNode[] GetTableIndex(string tableName) { if ((connString)) return null; List<TreeNode> nodes = new List<TreeNode>(); string connStr = connString; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(("exec sp_helpindex {0}", tableName), conn); = ; try { (); using (SqlDataReader MyReader = ()) { while (()) { TreeNode node = new TreeNode(MyReader[0].ToString(), 2, 2);/*Index name*/ = ("{0}{1}{2}", MyReader[2].ToString()/*index keys*/, , MyReader[1].ToString()/*Description*/); (node); } } } finally { (); } if(>0) return (); return null; }
9. Get the parameters of Table, View, Function, stored procedures, and Field information:
public string[] GetTableFields(string tableName) { return GetList(("select name from syscolumns where id =object_id('{0}')", tableName)); }
10. Get the detailed definition of each field in Table:
public TreeNode[] GetTableFieldsDefinition(string TableName) { if ((connString)) return null; string connStr = connString; List<TreeNode> nodes = new List<TreeNode>(); SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(("select ,,, from syscolumns a,systypes b,sysobjects d where = and = and ='U' and =object_id('{0}')", TableName), conn); = ; try { (); using (SqlDataReader MyReader = ()) { while (()) { TreeNode node = new TreeNode(MyReader[0].ToString(), 2, 2); = ("Type: {0}{1}Length: {2}{1}Nullable: {3}", MyReader[1].ToString()/*type*/, , MyReader[2].ToString()/*length*/, (MyReader[3])); (node); } } if ( > 0) return (); return null; } finally { (); } }
11. Get the stored procedure content:
Similar to "8. Get the index index information of the table", the SQL statement is:EXEC Sp_HelpText 'Stored Procedure Name'
12. Get the view View definition:
Similar to "8. Get the index index information of the table", the SQL statement is:EXEC Sp_HelpText 'View Name'
(The above code can be used in the code generator, listing all information about the database)
For more information about C# related content, please check out the topic of this site:Summary of common database operation techniques in C#》、《Tutorial on the usage of common C# controls》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.