冷风.NET

    ---默默無聞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

連接任何數據庫類(四)

Posted on 2004-12-27 16:07  冷风.net  阅读(557)  评论(0)    收藏  举报
using System;
using System.Data;

namespace DBSpaceName
{
    
/// <summary>
    
/// 表結構屬性/存儲過程參數
    
/// </summary>

    public struct DBStructure
    
{
        
/// <summary>字段名</summary>
        public string name;
        
/// <summary>類型 </summary>
        public string type;
        
/// <summary>長度</summary>
        public int length;
        
/// <summary>是否為空</summary>
        public bool notNull;
        
/// <summary>是否輸出</summary>
        public bool isOut;
    }


    
/// <summary>
    
/// 數據庫結構類型
    
/// </summary>

    public enum DBStructureType
    
{
        
/// <summary>表結構</summary>
        IsUserTable,    
        
/// <summary>存儲過程</summary>
        IsProcedure,
        
/// <summary>視圖</summary>
        IsView
    }


    
/// <summary>
    
/// 數據庫結構
    
/// </summary>

    public class DataBaseStructure
    
{
        
public DataBaseStructure(){}

        
/// <summary>
        
/// 獲得sql數據庫的結構
        
/// </summary>
        
/// <param name="objName">sql數據庫對像名</param>
        
/// <param name="objType">sql數據庫對像類型</param>
        
/// <returns>sql數據庫結構</returns>

        public DBStructure[] GetDBStructureSql(string objName,DBStructureType objType)
        
{
            DBStructure[] returnStructure;    
//定義反回結構
            string sqlString = "select a.name,a.length,";
            sqlString 
+= "xtype=(select top 1 b.name from dbo.systypes b where b.xtype=a.xtype),";
            sqlString 
+= "isnullable=(case when a.isnullable=1 then 'true' else 'false' end),";
            sqlString 
+= "isoutparam=(case when a.isoutparam=1 then 'true' else 'false' end) ";
            sqlString 
+= "from dbo.syscolumns a where id=object_id(N'[dbo].[" + objName + "]') ";
            sqlString 
+= "and OBJECTPROPERTY(id, N'"+ objType + "') = 1";
            DataSet ds 
= ExecuteDB.ExcuteSqlDataSet(sqlString);
            
try
            
{
                DataTable dt 
= ds.Tables[0];
                returnStructure 
= new DBStructure[dt.Rows.Count];
                
for(int i=0; i<dt.Rows.Count; i++)
                
{
                    returnStructure[i].name 
= dt.Rows[i]["name"].ToString();
                    returnStructure[i].type 
= DataBaseStructure.SqlToC_DataType(dt.Rows[i]["xType"].ToString());
                    returnStructure[i].length 
= Int32.Parse(dt.Rows[i]["length"].ToString());
                    returnStructure[i].notNull 
= bool.Parse(dt.Rows[i]["isnullable"].ToString());
                    returnStructure[i].isOut 
= bool.Parse(dt.Rows[i]["isoutparam"].ToString());
                }

            }

            
catch
            
{
                returnStructure 
= null;
            }

            
return returnStructure;
        }


        
/// <summary>
        
/// 獲得表結構
        
/// </summary>
        
/// <param name="tableName">表名</param>
        
/// <returns>表結構</returns>

        public static DBStructure[] TableStructure(string tableName)
        
{
            DBStructure[] returnStructure;
            DataBaseStructure dbStructure 
= new DataBaseStructure();
            returnStructure 
= dbStructure.GetDBStructureSql(tableName,DBStructureType.IsUserTable);
            
return returnStructure;
        }


        
/// <summary>
        
/// 獲得存儲過程的參數
        
/// </summary>
        
/// <param name="procedureName">存儲過程名</param>
        
/// <returns>存儲過程的參數</returns>

        public static DBStructure[] ProcedureParametes(string procedureName)
        
{
            DBStructure[] returnStructure;
            DataBaseStructure dbStructure 
= new DataBaseStructure();
            returnStructure 
= dbStructure.GetDBStructureSql(procedureName,DBStructureType.IsProcedure);
            
return returnStructure;
        }


        
/// <summary>
        
/// 將sql數據類型轉換成C#類型
        
/// </summary>
        
/// <param name="sqlType">sql數據類型</param>
        
/// <returns>C#數據類型</returns>

        public static string SqlToC_DataType(string sqlType)
        
{
            
string cType = null;
            
switch(sqlType.ToLower())
            
{
                
case "bigint":            //整數 (完整數字) 資料從 -2^63 (-9223372036854775808) 至 2^63-1 (9223372036854775807)。
                case "int":                //整數 (完整數字) 資料從 -2^31 (-2,147,483,648) 至 2^31 - 1 (2,147,483,647),而
                case "smallint":        //整數資料,從 2^15 (-32,768) 到 2^15 - 1 (32,767)。
                case "trnyint":            //整數資料,從 0 到 255。
                case "bit":                //整數資料,其值為 1 或 0。
                    cType = "int";
                    
break;
                
case "decimal":            //固定位數及小數位數的數字 (Numeric) 資料是從 -10^38 +1 到 10^38 1。
                case "numeric":            //固定位數及小數位數的數字 (Numeric) 資料是從 -10^38 +1 到 10^38 1。
                case "money":            //貨幣資料值從 -2^63 (-922,337,203,685,477.5808) 到2^63 - 1 (+922,337,203,685,477.5807),精確度到千分之十貨幣單位。
                case "smallmoney":        //貨幣資料值從 -214.748,3648 到 +214,748.3647,精確度到千分之十貨幣單位。
                case "float":            //浮點位數的數字資料,從 -1.79E + 308 到 1.79E + 308。
                    cType = "float";
                    
break;
                
case "real":            //浮點位數的數字資料,從 -3.40E + 38 到 3.40E + 38。
                    cType = "double";
                    
break;
                
case "datetime":        //日期與時間資料,從 1753 年 1 月 1 日到 9999 年 12 月 31 日,精確度為三百分之一秒,即 3.33 毫秒 (millisecond)。
                case "smalldatetime":    //日期與時間資料,從 1900 年 1 月 1 日到 2079 年 6 月 6 日,精確度為一分鐘。
                    cType = "DateTime";
                    
break;
                
case "char":            //固定長度的非 Unicode 字元資料,最大長度為 8,000 個字元。
                case "nchar":            //固定長度的非 Unicode 資料,最大長度為 4,000 個字元。
                case "varchar":            //可變長度的非 Unicode 資料,最大長度為 8,000 個字元。
                case "nvarchar":        //可變長度的 Unicode 資料,最大長度為 4,000 個字元。sysname 是一個系統支援的使用者自訂資料型別,為 nvarchar(128) 的同義資料表,用來參考資料庫物件名稱。
                case "text":            //可變長度的非 Unicode 資料,最大長度為 2^31 - 1 (2,147,483,647) 個字元。
                case "ntext":            //可變長度的 Unicode 資料,最大長度為 2^30 - 1 (1,073,741,823) 個字元。
                    cType = "string";
                    
break;
                
case "binary":            //固定長度的二進位資料,最大長度為 8,000 個位元組。
                case "varbinary":        //可變長度的二進位資料,最大長度為 8,000 個位元組。
                case "image":            //可變長度的二進位資料,最大長度為 2^31 - 1 (2,147,483,647) 個位元組。
                    cType = "char";
                    
break;
                
default:
                    cType 
= null;
                    
break;
            }

            
return cType;
        }

    }

}