public class DBHelper
{
public static SqlConnection SqlConnection = new SqlConnection("Server=服务器名称;Uid=登录名;Pwd=密码;Database=数据库名;");
/*(注:错误的服务器名称:DE\SQL
正确的服务器名称:DE\\SQL
)*/
//打开数据库
public static void GetOpen()
{
if (SqlConnection.State == ConnectionState.Closed)
{
SqlConnection.Open();
}
}
//关闭数据库
public static void GetClose()
{
if (SqlConnection.State == ConnectionState.Open)
{
SqlConnection.Close();
}
}
//返回表格
public static DataTable GetTable(string sqlcom)
{
SqlDataAdapter sqlData = new SqlDataAdapter(sqlcom, SqlConnection);
DataTable dt = new DataTable();
sqlData.Fill(dt);
return dt;
}
//添加语句
public static bool ExecuteNonQuery(string sqlcom)
{
GetOpen();
SqlCommand sqlCommand = new SqlCommand(sqlcom, SqlConnection);
int a = sqlCommand.ExecuteNonQuery();
GetClose();
if (a > 0)
{
return true;
}
else
{
return false;
}
}
}
添加的引用:using System.Data.SqlClient;
using System.Data;