关于连接数据库时,报表0不存在,字段id不存在等等,写了cs文件-帮忙解决一下谢谢
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
namespace NewMyCRM.NewDBUtility
{
public class NewDBNetHelper
{
//连接数据库
private static SqlConnection connection;
//连接数据库
private static string sqlCon = "server=127.0.0.1;database=ghycw;uid=sa;pwd=sasa;Max Pool Size = 512";
//执行不带参数的非查询语句
public static int ExecuteCommand(string sql)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand scmd = new SqlCommand(sql, connection);
int row = scmd.ExecuteNonQuery();
return row;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
//执行带参数的非查询语句
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand scmd = new SqlCommand(sql, connection);
scmd.Parameters.AddRange(values);
int row = scmd.ExecuteNonQuery();
return row;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
//执行不带参数的查询语句
//执行带参数的查询语句
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand scmd = new SqlCommand(sql, connection);
scmd.Parameters.AddRange(values);
SqlDataReader reader = scmd.ExecuteReader();
return reader;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
//执行不带参数的判断是否有查询结果的语句
public static int GetScalar(string safeSql)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand cmd = new SqlCommand(safeSql, connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
public static string GetCustomerName(string safeSql)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand cmd = new SqlCommand(safeSql, connection);
string result = Convert.ToString(cmd.ExecuteScalar());
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
public static string GetCount(string safeSql)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand cmd = new SqlCommand(safeSql, connection);
string result = Convert.ToString(cmd.ExecuteScalar());
//cmd.CommandText = safeSql;
// cmd.Connection = Connection;
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
//执行带参数的判断是否有查询结果的语句
public static int GetScalar(string safeSql, params SqlParameter[] values)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlCommand cmd = new SqlCommand(safeSql, connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
//此方法用于以DataTable形式返回多条查询结果
public static DataTable GetDataSet(string sql)
{
try
{
using (connection = new SqlConnection(sqlCon))
{
connection.Open();
//Do something
SqlDataAdapter sda = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
else
{
return null;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
finally
{
connection.Close();
}
}
}
}