'Object'不包含'Rows'的定义,也没有扩展名 方法'Rows'接受'object'类型的第一个参数可能是 发现您是否缺少using指令或程序集引用?
代码如下:
public partial class Default3 : System.Web.UI.Page
{
static SqlConnection con = new SqlConnection(@"connectionString");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ddlImg.DataSource = GetData("SELECT EmpID, EmpName FROM Tbl_Emp");
ddlImg.DataTextField = "EmpName";
ddlImg.DataValueField = "EmpID";
ddlImg.DataBind();
}
}
private object GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["tbFiles2ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = ("INSERT INTO Tbl_Emp(EmpID, EmpName, EmpPic ) VALUES('"+TextBox1.Text+"','"+TextBox2.Text+"','" + FileUpload1 + "')");
cmd.ExecuteNonQuery();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string id = ddlImg.SelectedItem.Value;
Image1.Visible = id != "0";
if (id != "0")
{
byte[] bytes = (byte[])GetData("SELECT Data FROM Tbl_Emp WHERE Emp_ID =" + id).Rows[0]["Data"]; // Error popup here
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
}
}
}
答案 0 :(得分:2)
问题是GetData
返回没有object
属性的Rows
。因此,您在以下位置收到编译器错误:
...GetData("SELECT Data FROM Tbl_Emp WHERE Emp_ID =" + id).Rows[0]...
返回DataTable
代替:
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["tbFiles2ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
return dt;
}
请注意,
static
连接(更不用说ASP.NET),请使用using
语句尽快关闭连接。阅读:ExecuteReader requires an open and available Connection. The connection's current state is Connecting 答案 1 :(得分:1)
将getdata()
功能签名更改为
private DataTable GetData(string query){
}