代码有问题吗??它不起作用并抛出异常对象参考。未设置为对象实例。异常位于代码行字符串 DbConnectionString = Connection.ConnectionString处的 datageneric.cs 文件中;
连接.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
public static class Connection
{
private static string constr = "server=.;database=foood;";
public static string ConnectionString
{
get
{
return constr;
}
set
{
constr = value;
}
}
public static string ProviderName { get; set; }
static Connection()
{
ConnectionString =WebConfigurationManager.ConnectionStrings["constring"].ConnectionString;
ProviderName = WebConfigurationManager.ConnectionStrings["constring"].ProviderName;
}
}
DataGeneric.cs
public static DbCommand CreateCommand()
{
DbCommand cmd=null;
try
{
string DbConnectionString = Connection.ConnectionString;
string DbProviderName = Connection.ProviderName;
DbProviderFactory factory = DbProviderFactories.GetFactory(DbProviderName);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = DbConnectionString;
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
}
catch (Exception ex) { }
return cmd;
}
获取数据.cs
public static DataTable gimmedata()
{
string Query = "select * from [slider]";
DbCommand cmd = DataGeneric.CreateCommand();
cmd.CommandText = Query;
DataTable dt = DataGeneric.ExecuteSelectCommand(cmd);
return dt;
}
默认.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetData.gimmedata();
GridView1.DataSource = dt;
GridView1.DataBind();
}