数据库读写 CDataBase
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SelectCourse
{
class CDataBase
{
public static string connStr;
public static SqlConnection conn = new SqlConnection(connStr);
public static DataSet GetDataFromDB(string sqlStr)
{
conn.ConnectionString = connStr;
conn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(sqlStr, conn);
DataSet myDataSet = new DataSet();
myDataSet.Clear();
myAdapter.Fill(myDataSet);
conn.Close();
if (myDataSet.Tables[0].Rows.Count != 0)
{
return myDataSet;
}
else
{
return null;
}
}
public static bool UpdateDB(string sqlStr) //”写”数据库的静态方法
{
conn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, conn);// 定义数据库的命令对象
myCmd.CommandType = CommandType.Text;// 设置command的commandtyle属性
myCmd.ExecuteNonQuery();//执行SQL语句
conn.Close();
return true;// 数据更新成功,返回true
}
}
}