c#中SQL Server 資料庫的讀寫語句
資料庫讀寫 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
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
{
conn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, conn);// 定義資料庫的命令物件
myCmd.CommandType = CommandType.Text;// 設定command的commandtyle屬性
myCmd.ExecuteNonQuery();//執行SQL語句
conn.Close();
return true;// 資料更新成功,返回true
}
}
}