有谁有连数据库的教程啊,我搞不定啊!1
解决方案 »
- 做一企业网站需求分析需要注意什么?
- 加急!csv文件或txt文件通过asp.net上传文件,再把文件里的数据导入是怎么实现的?
- 求一道面试题的答案
- ASP.NET中怎样保持页面不闪动?
- 能不能对Gridview的单元格单独设置边框
- ActiveX component can't create object: scripting.FileSystemObject
- 如何加密在地址栏中所显示的url后面的参数?
- 项目全部做完了~要把系统拿到别的机器上演示,SQL2000数据库怎么导出啊?
- 难题求教:有没有办法可以删除页面的缓存?谢谢!!!
- 索取一个学生档案管理系统(最好是Asp.net版本的,asp也可以)
- 如何导入word文档到asp.net页面??请各位高手帮忙,急在线等
- 如何在C#项目中使用java接口
看看,都有了
/// 打开数据库连接
/// </summary>
/// <param name="connStr">连接字符串</param>
public static void openConnection(String connStr)
{
if (myConnection.State == ConnectionState.Closed)
{
myConnection.ConnectionString = connStr;
myCommand.Connection = myConnection;
try
{
myConnection.Open();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
}
/// 关闭当前数据库连接
/// </summary>
public static void closeConnection()
{
if(myConnection.State == ConnectionState.Open)
myConnection.Close();
myConnection.Dispose();
myCommand.Dispose();
} /// <summary>
/// 执行SQL语句,不返回结果集
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <param name="cmdStr">SQL语句</param>
public static void ExecuteSql(String connStr, String cmdStr)
{
try
{
openConnection(connStr);
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = cmdStr;
myCommand.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 执行SQL语句,并返回DataSet
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <param name="cmdStr">SQL语句</param>
/// <param name="dataTableName">DataTableName</param>
public static DataSet ExecuteSqlReturnDataset(String connStr, String cmdStr, string dataTableName)
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
openConnection(connStr);
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = cmdStr;
myAdapter.SelectCommand = myCommand;
myAdapter.Fill(ds, dataTableName);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}