频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

vs.net+c#程序快速开发数据库简单操作

作者: WindElf 出处:天新网  (  ) 砖  (  ) 好  评论 ( ) 条  进入论坛
更新时间:2007-01-11 12:50
关 键 词:.Net  VS  c#  数据库
阅读提示:本文简单给出了使用vs.net+c#程序来开发数据库简单操作的步骤和源代码。供大家参考!

一、已有数据库(SQL)表,快速生成对应存储过程的工具。

SQLPilot.exe

二、已有数据库表,快速生成列表、基本查询、高级查询、插入新行、删除、编辑ASP.net页面的工具ASP.NET Maker。

可参看网址:http://www.hkvstore.com/aspnetmaker/

数据库操作类,以下是C#代码:

源代码文件如下:

using System;
using System.Web.UI;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;
namespace ns_db
{
public class c_db: UserControl
{

//********************************
//  返回数据库连接字符串
//********************************

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 Object
SqlConnection oConn = new SqlConnection(sConn);

// Create a new DataAdapter using the Connection Object and SQL statement
SqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);

// Create a new DataSet Object to fill with Data
DataSet oDs = new DataSet();

// Fill the DataSet with Data from the DataAdapter Object
oDa.Fill(oDs, "ewDataSet");
return oDs.Tables[0].DefaultView;
}
catch (SqlException oErr)
{
Session["dberrmsg"] = ewDataErrorMessage(oErr);
return null;
}
} // End ewDataView

//*********************************
//  返回一个 DataView for Linking
//*********************************

public DataView ewDataViewLink(string sConn, string sTable/*表名*/,
string sLnkFld, string sDispFld,
string sDispFld2, string sFilterFld,
string sOrderBy/*排序列*/, string sOrderType/*排序规则*/,
bool bDistinct/*是否剔除重复值*/, string sFilter)
{
string sSQL;
try
{

// Construct SQL statement
sSQL = "SELECT";
if (bDistinct)
{
sSQL += " DISTINCT";
}
sSQL += " [" + sLnkFld + "], [" + sDispFld + "]";
if (sDispFld2 != "")
{
sSQL += ", [" + sDispFld2 + "]";
}
if (sFilterFld != "")
{
sSQL += ", [" + sFilterFld + "]";
}
sSQL += " FROM [" + sTable + "]";
if (sFilter != "")
{
sSQL += " WHERE " + sFilter;
}
if (sOrderBy != "")
{
sSQL += " ORDER BY [" + sOrderBy + "] " + sOrderType;
}

// Create a new Connection Object using the Connection String
SqlConnection oConn = new SqlConnection(sConn);

// Create a new DataAdapter using the Connection Object and SQL statement
SqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);

// Create a new DataSet Object to fill with Data
DataSet oDs = new DataSet();

// Fill the DataSet with Data from the DataAdapter Object
oDa.Fill(oDs, "ewDataSet");

// Create the TextField and ValueField Columns
oDs.Tables[0].Columns.Add("ewValueField",Type.GetType("System.String"),

"[" + sLnkFld + "]");
if (sDispFld2 == "")
{
oDs.Tables[0].Columns.Add("ewTextField",Type.GetType("System.String"),

"[" + sDispFld + "]");
}
else
{
oDs.Tables[0].Columns.Add("ewTextField",Type.GetType("System.String"),

"[" + sDispFld + "] +

', ' + [" + sDispFld2 + "]");
}
return oDs.Tables[0].DefaultView;
}
catch (SqlException oErr)
{
Session["dberrmsg"] = ewDataErrorMessage(oErr);
return null;
}
} // End ewDataViewLink

//*********************************************************
//  根据 Connection String & SQL 返回 Records 的数量
//*********************************************************

public int ewRecordCount(string sConn, string sTable, string sWhere)
{
string sSQL;
try
{

// Construct SQL
sSQL = "SELECT COUNT(*) FROM [" + sTable + "]";
if (sWhere != "")
{
sSQL += " WHERE " + sWhere;
}

// Create a new Connection Object using the Connection String
SqlConnection oConn = new SqlConnection(sConn);

// Create a new DataAdapter using the Connection Object and SQL statement
SqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);

// Create a new DataSet object to fill with Data
DataSet oDs = new DataSet();

// Fill the DataSet with Data from the DataAdapter Object
oDa.Fill(oDs, "ewDataSet");
return Convert.ToInt32(oDs.Tables[0].Rows[0][0]);
}
catch (SqlException oErr)
{
Session["dberrmsg"] = ewDataErrorMessage(oErr);
return 0;
}
} // End ewRecordCount

//***********************************************************
//  返回 1-page DataView 根据 Connection String & SQL
//***********************************************************

public DataView ewDataViewPage(string sConn, string sSQL,
int iCurrentRec, int iPageSize)
{
try
{

// Create a new Connection Object using the Connection String
SqlConnection oConn = new SqlConnection(sConn);

// Create a new DataAdapter using the Connection Object and SQL statement
SqlDataAdapter oDa = new SqlDataAdapter(sSQL, oConn);

// Create a new DataSet object to fill with Data
DataSet oDs = new DataSet();

// Fill the DataSet with Data from the DataAdapter Object
oDa.Fill(oDs, iCurrentRec, iPageSize, "ewDataSet");
return oDs.Tables[0].DefaultView;
}
catch (SqlException oErr)
{
Session["dberrmsg"] = ewDataErrorMessage(oErr);
return null;
}
} // End ewDataViewPage

//*************************************************
//  Return a DataReader based on Connection & SQL
//*************************************************

public SqlDataReader ewDataReader(SqlConnection oConn, string sSQL)
{
try
{

// Create a DataReader Object
SqlDataReader oDr;

// Create a new Command Object using the Connection and SQL statement
SqlCommand oCmd = new SqlCommand(sSQL, oConn);

// Execute the SQL statement against the Command to get the DataReader
oDr = oCmd.ExecuteReader();
return oDr;
}
catch (SqlException oErr)
{
Session["dberrmsg"] = ewDataErrorMessage(oErr);
return null;
}
} // End ewDataReader

//**********************************************
//  Return Error Message based on Error Object
//**********************************************

public string ewDataErrorMessage(SqlException oErr)
{
string sDbErrMsg;
sDbErrMsg = "";
for (int i = 0; i <= oErr.Errors.Count - 1; i++)
{
sDbErrMsg += "Message: " + oErr.Errors[i].Message + "
"
+ "Line Number: " + oErr.Errors[i].LineNumber + "
"
+ "Source: " + oErr.Errors[i].Source + "
"
+ "Procedure: " + oErr.Errors[i].Procedure + "
";
}
return sDbErrMsg;
} // End ewDataErrorMessage

//***************************
//  Return Upload File Name
//***************************

public string ewUploadFileName(string sFileName)
{
string sOutFileName;

// Amend your logic here
sOutFileName = sFileName;

// Return computed output file name
return sOutFileName;
} // End ewUploadFileName

//******************************************************
//  Return Formatted Number similar to VB FormatNumber
//  - IncludeLeadingDigit is not supported
//******************************************************

public string ewFormatNumber(object oNo, int iDecPlace, int iUseParen,

int iGroupDigits)
{
NumberFormatInfo oNfi = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();
oNfi.NumberDecimalDigits = iDecPlace; // NumDigitsAfterDecimal

// IncludeLeadingDigit: not used
if (iUseParen == -1) // UseParensForNegativeNumbers: True
{
oNfi.NumberNegativePattern = 0;
}
else if (iUseParen == 0) // UseParensForNegativeNumbers: False
{
oNfi.NumberNegativePattern = 1;
}
if (iGroupDigits == -1) // GroupDigits: True
{
oNfi.NumberGroupSeparator = ",";
}
else if (iGroupDigits == 0) // GroupDigits: False
{
oNfi.NumberGroupSeparator = "";
}

// Cast for different data types
if (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) // 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 ((decimal) 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 = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();
oNfi.CurrencyDecimalDigits = iDecPlace; // NumDigitsAfterDecimal

// IncludeLeadingDigit: not used
if (iUseParen == -1) // UseParensForNegativeNumbers: True
{
oNfi.CurrencyNegativePattern = 0;
}
else if (iUseParen == 0) // UseParensForNegativeNumbers: False
{
oNfi.CurrencyNegativePattern = 1;
}
if (iGroupDigits == -1) // GroupDigits: True
{
oNfi.CurrencyGroupSeparator = ",";
}
else if (iGroupDigits == 0) // GroupDigits: False
{
oNfi.CurrencyGroupSeparator = "";
}

// Cast for different data types
if (oNo is short) // short
{ return ((short) oNo).ToString("c",oNfi); }
else if (oNo is ushort) // ushort
{ return ((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); }
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 to 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

// IncludeLeadingDigit: not used
if (iUseParen == -1) // UseParensForNegativeNumbers: True
{
oNfi.CurrencyNegativePattern = 0;
}
else if (iUseParen == 0) // UseParensForNegativeNumbers: False
{
oNfi.CurrencyNegativePattern = 1;
}
if (iGroupDigits == -1) // GroupDigits: True
{
oNfi.CurrencyGroupSeparator = ",";
}
else if (iGroupDigits == 0) // GroupDigits: False
{
oNfi.CurrencyGroupSeparator = "";
}

// Cast for different data types
if (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 is 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

(责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)


发表
查看
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看52473次)
·UML类图详解 (查看46951次)
·Java编程开发手册 (查看25172次)
·UML统一建模语言 (查看24155次)
·C#技术开发指南 (查看22515次)
·Java编程开发手册 (1195个砖)
·Java基础教程 (429个砖)
·C#技术开发指南 (304个砖)
·PB开发教程 (220个砖)
·.NET开发手册 (217个砖)
·Java编程开发手册 (653个好)
·Java基础教程 (569个好)
·.NET开发手册 (251个好)
·PB开发教程 (209个好)
·Delphi开发技术手册 (174个好)
订阅技术快讯
电子杂志下载
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。
名称:Vista精品应用黄皮书
简介:《Vista精品应用黄皮书》囊括了Vista的各方面内容。此次的精简版,是将里面的内容做了提取,便于用户下载和使用。内容包含了各种Vista的安装与实施、技巧与解析以及各种Vista相关学习文档和相关软件的安全下载。该电子书是了解和应用Vista人员必备的工具手册,并且也是第一本
名称:2006中国IT论坛精品集合
简介:本书由“51CTO论坛推广联盟”制作完成。书中所有内容均来自各联盟成员的论坛(网站)。制作本书的目的是为了集中大家的优势资源,将更多更精彩的内容带给广大技术爱好者。本书是联盟成立以来制作的第一本书。
关键字阅读
频道精选
主编信箱 热线:010-66476606 告诉我们您想看的:专题 文章
关于我们 | 诚聘英才 | 联系我们 | 网站大事 | 意见反馈 | 网站地图
Copyright©2005-2007 51CTO.COM 版权所有