dal里面
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace dal
{
public class DALClass
{
public static string connectionstring = @"Data Source=.\SQLEXPRESS;integrated security=SSPI;database=tupian";
public static DataSet dt(string sql)
{
SqlConnection connection = new SqlConnection(sql,connectionstring);
SqlCommand command = new SqlCommand(sql,connection);
try
{
connection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=command;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
catch(SqlException ex)
{
throw ex;
}
}
}
}
试试这种写法,你那种好像有问题
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace dal
{
public class DALClass
{
public static string connectionstring = @"Data Source=.\SQLEXPRESS;integrated security=SSPI;database=tupian";
public static DataSet dt(string sql)
{
SqlConnection connection = new SqlConnection(sql,connectionstring);
SqlCommand command = new SqlCommand(sql,connection);
try
{
connection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=command;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
catch(SqlException ex)
{
throw ex;
}
}
}
}
试试这种写法,你那种好像有问题
{
SqlConnection connection = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = sql;
try
{
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds,dst);
return ds.Tables[dst];
}
catch(SqlException ex)
{
connection.Close();
throw ex;
}
}
string strsql = string.Format("SELECT mingcheng,yonghuming,fabushijian,dianji FROM tupian WHERE shenli=1 AND gongkai=0 ORDER BY dianji DESC");
string sds = "tupian.mingcheng,tupian.yonghuming,tupian.fabushijian,tupian.dianji";
GridView1.DataSource = dal.DALClass.dt(strsql,sds);
GridView1.DataBind();
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace dal
{
public class DALClass
{
public static string connectionstring = @"Data Source=.\SQLEXPRESS;integrated security=SSPI;database=tupian";
public static DataSet dt(string sql)
{
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionstring;
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,connection);
DataSet ds = new DataSet();
da.Fill(ds,"表名");
return ds;
}
catch(SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
}
页面里
protected void Page_Load(object sender, EventArgs e)
{
string strsql = string.Format("SELECT mingcheng,yonghuming,fabushijian,dianji FROM tupian WHERE shenli=1 AND gongkai=0 ORDER BY dianji DESC");
this.GridView1.DataSource = dal.DALClass.dt(strsql);
this.DataBind();
}
以上代码中,有一个问题哦,就是你连接字符串中的database是tupian ,怎么你sql语句中也有个表名称叫tupian 呢?
还有一点就是你数据库表中没有记录,当然查不出结果显示了啊!
this.GridView1.DataBind(); // 加上这个