C#讀取SqlServer預存程序及參數《轉》

來源:互聯網
上載者:User

讀取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();

    }

              

}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.