C Sharp数据库简单操作
《C Sharp数据库简单操作》由会员分享,可在线阅读,更多相关《C Sharp数据库简单操作(11页珍藏版)》请在装配图网上搜索。
1、c#快速开发数据库简单操作一、已有数据库(SQL)表,快速生成对应存储过程的工具。SQLPilot.exe二、已有数据库表,快速生成列表、基本查询、高级查询、插入新行、删除、编辑ASP.net页面的工具ASP.NET Maker。可参看网址:数据库操作类,以下是C#代码:源代码文件如下:using System;using System.Web.UI;using System.Globalization;using System.Data;using System.Data.SqlClient;namespace ns_dbpublic class c_db: UserControl/*/ 返
2、回数据库连接字符串/*public string ewConnStr()return Password=*;Persist Security Info=True;User ID=sa;Initial Catalog=*;Data Source=(Local); / End ewConnStr/*/ 返回一个 DataView 根据 Connection String & SQL/*public DataView ewDataView(string sConn, string sSQL)try/ Create a new Connection ObjectSqlConnection oConn
3、= new SqlConnection(sConn);/ Create a new DataAdapter using the Connection Object and SQL statementSqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);/ Create a new DataSet Object to fill with DataDataSet oDs = new DataSet();/ Fill the DataSet with Data from the DataAdapter ObjectoDa.Fill(oDs, ewD
4、ataSet);return oDs.Tables0.DefaultView;catch (SqlException oErr)Sessiondberrmsg = ewDataErrorMessage(oErr);return null; / End ewDataView/*/ 返回一个 DataView for Linking/*public DataView ewDataViewLink(string sConn, string sTable/*表名*/,string sLnkFld, string sDispFld,string sDispFld2, string sFilterFld,
5、string sOrderBy/*排序列*/, string sOrderType/*排序规则*/,bool bDistinct/*是否剔除重复值*/, string sFilter)string sSQL;try/ Construct SQL statementsSQL = SELECT;if (bDistinct)sSQL += DISTINCT;sSQL += + sLnkFld + , + sDispFld + ;if (sDispFld2 != )sSQL += , + sDispFld2 + ;if (sFilterFld != )sSQL += , + sFilterFld +
6、;sSQL += FROM + sTable + ;if (sFilter != )sSQL += WHERE + sFilter;if (sOrderBy != )sSQL += ORDER BY + sOrderBy + + sOrderType;/ Create a new Connection Object using the Connection StringSqlConnection oConn = new SqlConnection(sConn);/ Create a new DataAdapter using the Connection Object and SQL stat
7、ementSqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);/ Create a new DataSet Object to fill with DataDataSet oDs = new DataSet();/ Fill the DataSet with Data from the DataAdapter ObjectoDa.Fill(oDs, ewDataSet);/ Create the TextField and ValueField ColumnsoDs.Tables0.Columns.Add(ewValueField,Type
8、.GetType(System.String), + sLnkFld + );if (sDispFld2 = )oDs.Tables0.Columns.Add(ewTextField,Type.GetType(System.String), + sDispFld + );elseoDs.Tables0.Columns.Add(ewTextField,Type.GetType(System.String), + sDispFld + + , + + sDispFld2 + );return oDs.Tables0.DefaultView;catch (SqlException oErr)Sess
9、iondberrmsg = ewDataErrorMessage(oErr);return null; / End ewDataViewLink/*/ 根据 Connection String & SQL 返回 Records 的数量/*public int ewRecordCount(string sConn, string sTable, string sWhere)string sSQL;try/ Construct SQLsSQL = SELECT COUNT(*) FROM + sTable + ;if (sWhere != )sSQL += WHERE + sWhere;/ Cre
10、ate a new Connection Object using the Connection StringSqlConnection oConn = new SqlConnection(sConn);/ Create a new DataAdapter using the Connection Object and SQL statementSqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);/ Create a new DataSet object to fill with DataDataSet oDs = new DataSet(
11、);/ Fill the DataSet with Data from the DataAdapter ObjectoDa.Fill(oDs, ewDataSet);return Convert.ToInt32(oDs.Tables0.Rows00);catch (SqlException oErr)Sessiondberrmsg = ewDataErrorMessage(oErr);return 0; / End ewRecordCount/*/ 返回 1-page DataView 根据 Connection String & SQL/*public DataView ewDataView
12、Page(string sConn, string sSQL,int iCurrentRec, int iPageSize)try/ Create a new Connection Object using the Connection StringSqlConnection oConn = new SqlConnection(sConn);/ Create a new DataAdapter using the Connection Object and SQL statementSqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);/ C
13、reate a new DataSet object to fill with DataDataSet oDs = new DataSet();/ Fill the DataSet with Data from the DataAdapter ObjectoDa.Fill(oDs, iCurrentRec, iPageSize, ewDataSet);return oDs.Tables0.DefaultView;catch (SqlException oErr)Sessiondberrmsg = ewDataErrorMessage(oErr);return null; / End ewDat
14、aViewPage/*/ Return a DataReader based on Connection & SQL/*public SqlDataReader ewDataReader(SqlConnection oConn, string sSQL)try/ Create a DataReader ObjectSqlDataReader oDr;/ Create a new Command Object using the Connection and SQL statementSqlCommand oCmd = new SqlCommand(sSQL, oConn);/ Execute
15、the SQL statement against the Command to get the DataReaderoDr = oCmd.ExecuteReader();return oDr;catch (SqlException oErr)Sessiondberrmsg = ewDataErrorMessage(oErr);return null; / End ewDataReader/*/ Return Error Message based on Error Object/*public string ewDataErrorMessage(SqlException oErr)strin
16、g sDbErrMsg;sDbErrMsg = ;for (int i = 0; i = oErr.Errors.Count - 1; i+)sDbErrMsg += Message: + oErr.Errorsi.Message + + Line Number: + oErr.Errorsi.LineNumber + + Source: + oErr.Errorsi.Source + + Procedure: + oErr.Errorsi.Procedure + ;return sDbErrMsg; / End ewDataErrorMessage/*/ Return Upload File
17、 Name/*public string ewUploadFileName(string sFileName)string sOutFileName;/ Amend your logic heresOutFileName = sFileName;/ Return computed output file namereturn sOutFileName; / End ewUploadFileName/*/ Return Formatted Number similar to VB FormatNumber/ - IncludeLeadingDigit is not supported/*publ
18、ic string ewFormatNumber(object oNo, int iDecPlace, int iUseParen, int iGroupDigits)NumberFormatInfo oNfi = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();oNfi.NumberDecimalDigits = iDecPlace; / NumDigitsAfterDecimal/ IncludeLeadingDigit: not usedif (iUseParen = -1) / UseParensForNegativeNu
19、mbers: TrueoNfi.NumberNegativePattern = 0;else if (iUseParen = 0) / UseParensForNegativeNumbers: FalseoNfi.NumberNegativePattern = 1;if (iGroupDigits = -1) / GroupDigits: TrueoNfi.NumberGroupSeparator = ,;else if (iGroupDigits = 0) / GroupDigits: FalseoNfi.NumberGroupSeparator = ;/ Cast for differen
20、t data typesif (oNo is short) / short return (short) oNo).ToString(n,oNfi); else if (oNo is ushort) / ushort return (ushort) oNo).ToString(n,oNfi); else if (oNo is int) / int return (int) oNo).ToString(n,oNfi); else if (oNo is uint) / uint return (uint) oNo).ToString(n,oNfi); else if (oNo is long) /
21、 long return (long) oNo).ToString(n,oNfi); else if (oNo is ulong) / ulong return (ulong) oNo).ToString(n,oNfi); else if (oNo is float) / float return (float) oNo).ToString(n,oNfi); else if (oNo is double) / double return (double) oNo).ToString(n,oNfi); else if (oNo is decimal) / decimal return (deci
22、mal) oNo).ToString(n,oNfi); else return (decimal) oNo).ToString(n,oNfi); /*/ Return Formatted Currency similar to VB FormatCurrency/ - IncludeLeadingDigit is not supported/*public string ewFormatCurrency(object oNo, int iDecPlace, int iUseParen, int iGroupDigits)NumberFormatInfo oNfi = (NumberFormat
23、Info) NumberFormatInfo.CurrentInfo.Clone();oNfi.CurrencyDecimalDigits = iDecPlace; / NumDigitsAfterDecimal/ IncludeLeadingDigit: not usedif (iUseParen = -1) / UseParensForNegativeNumbers: TrueoNfi.CurrencyNegativePattern = 0;else if (iUseParen = 0) / UseParensForNegativeNumbers: FalseoNfi.CurrencyNe
24、gativePattern = 1;if (iGroupDigits = -1) / GroupDigits: TrueoNfi.CurrencyGroupSeparator = ,;else if (iGroupDigits = 0) / GroupDigits: FalseoNfi.CurrencyGroupSeparator = ;/ Cast for different data typesif (oNo is short) / short return (short) oNo).ToString(c,oNfi); else if (oNo is ushort) / ushort re
25、turn (ushort) oNo).ToString(c,oNfi); else if (oNo is int) / int return (int) oNo).ToString(c,oNfi); else if (oNo is uint) / uint return (uint) oNo).ToString(c,oNfi); else if (oNo is long) / long return (long) oNo).ToString(c,oNfi); else if (oNo is ulong) / ulong return (ulong) oNo).ToString(c,oNfi);
26、 else if (oNo is float) / float return (float) oNo).ToString(c,oNfi); else if (oNo is double) / double return (double) oNo).ToString(c,oNfi); else if (oNo is decimal) / decimal return (decimal) oNo).ToString(c,oNfi); else return (decimal) oNo).ToString(c,oNfi); /*/ Return Formatted Percent similar t
27、o VB FormatPercent/ - IncludeLeadingDigit is not supported/*public string ewFormatPercent(object oNo, int iDecPlace, int iUseParen, int iGroupDigits)NumberFormatInfo oNfi = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();oNfi.CurrencyDecimalDigits = iDecPlace; / NumDigitsAfterDecimal/ Includ
28、eLeadingDigit: not usedif (iUseParen = -1) / UseParensForNegativeNumbers: TrueoNfi.CurrencyNegativePattern = 0;else if (iUseParen = 0) / UseParensForNegativeNumbers: FalseoNfi.CurrencyNegativePattern = 1;if (iGroupDigits = -1) / GroupDigits: TrueoNfi.CurrencyGroupSeparator = ,;else if (iGroupDigits
29、= 0) / GroupDigits: FalseoNfi.CurrencyGroupSeparator = ;/ Cast for different data typesif (oNo is short) / short return (short) oNo).ToString(p,oNfi); else if (oNo is ushort) / ushort return (ushort) oNo).ToString(p,oNfi); else if (oNo is int) / int return (int) oNo).ToString(p,oNfi); else if (oNo i
30、s uint) / uint return (uint) oNo).ToString(p,oNfi); else if (oNo is long) / long return (long) oNo).ToString(p,oNfi); else if (oNo is ulong) / ulong return (ulong) oNo).ToString(p,oNfi); else if (oNo is float) / float return (float) oNo).ToString(p,oNfi); else if (oNo is double) / double return (double) oNo).ToString(p,oNfi); else if (oNo is decimal) / decimal return (decimal) oNo).ToString(p,oNfi); else return (decimal) oNo).ToString(p,oNfi); / End Class / End NameSpace
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。