从 WebForms 中的查询中获取指定值
Get specify value from Query in WebForms
我在从查询中获取指定值时遇到问题。我的 GridView 收到一行,但是当我尝试读取该值以将其放入某个变量时,它告诉我没有任何值。
else if (Request.QueryString.AllKeys.Contains("a"))
{
String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = ?";
connection.OPEN();
OleDbCommand sql_s = new OleDbCommand(sql_user, polaczenie);
sql_s.Parameters.add("?", OleDbType.Integer).Value = Request.QueryString["a"];
OleDbDataAdapter sqlDa = new OleDbDataAdapter(sql_s);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
GRID_VIEW1.DataSource = dt;
GRID_VIEW1.DataBind();
OleDbDataReader customer = sql_s.ExecuteReader();
// i want to get SHORT_NAME so that one below i think dosent work
// but any way it make me an Exception of any value not found but on GridView it shows me clearly one ROW
learerLabel.Text = (String) cmd.ExecuteScalar();
connection.Close();
}
我用很多方法来做,比如一些 Session 或 Reader,但我现在不记得我的所有尝试,但它总是告诉我,我的查询没有价值。我究竟做错了什么?谢谢
试试这个,把参数名换成@ID,参考下面的link msdn文档传递Parameter
SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @ID
sql_s.Parameters.add("@ID", OleDbType.Integer).Value = Request.QueryString["a"];
有关传递参数的更多详细信息,请参见以下 msdn
https://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.110).aspx
做一个 Class 这样的更好:
public class MyClass
{
public int Id { get; set; }
public string ShortName { get; set; }
public string Name { get; set; }
public int Regon { get; set; }
}
接下来要从您的查询中获取数据,试试这个:
CustomClass item = new CustomClass();
String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @id";
using (var conn = new OleDbConnection("connectionString"))
{
conn.Open();
using (var cmd = new OleDbCommand(sql_user, conn))
{
cmd.Parameters.Add("@id", OleDbType.Integer).Value = Request.QueryString["a"];
cmd.Prepare();
var reader = cmd.ExecuteReader();
if(reader.Read())
{
item.Id = (int)reader["ID"];
item.ShortName = reader["SHORT_NAME"].ToString();
item.Name= reader["NAME"].ToString();
item.Regon = (int)reader["REGON"];
}
conn.Close();
}
}
编辑:
如果你不想使用它,你需要改变 taht 行:
OleDbDataReader customer = sql_s.ExecuteReader();
至:
OleDbDataReader customer;
var dr = sql_s.ExecuteReader();
if (dr.Read())
customer = dr;
试试这个,然后写出来
我在从查询中获取指定值时遇到问题。我的 GridView 收到一行,但是当我尝试读取该值以将其放入某个变量时,它告诉我没有任何值。
else if (Request.QueryString.AllKeys.Contains("a"))
{
String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = ?";
connection.OPEN();
OleDbCommand sql_s = new OleDbCommand(sql_user, polaczenie);
sql_s.Parameters.add("?", OleDbType.Integer).Value = Request.QueryString["a"];
OleDbDataAdapter sqlDa = new OleDbDataAdapter(sql_s);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
GRID_VIEW1.DataSource = dt;
GRID_VIEW1.DataBind();
OleDbDataReader customer = sql_s.ExecuteReader();
// i want to get SHORT_NAME so that one below i think dosent work
// but any way it make me an Exception of any value not found but on GridView it shows me clearly one ROW
learerLabel.Text = (String) cmd.ExecuteScalar();
connection.Close();
}
我用很多方法来做,比如一些 Session 或 Reader,但我现在不记得我的所有尝试,但它总是告诉我,我的查询没有价值。我究竟做错了什么?谢谢
试试这个,把参数名换成@ID,参考下面的link msdn文档传递Parameter
SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @ID
sql_s.Parameters.add("@ID", OleDbType.Integer).Value = Request.QueryString["a"];
有关传递参数的更多详细信息,请参见以下 msdn https://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.110).aspx
做一个 Class 这样的更好:
public class MyClass
{
public int Id { get; set; }
public string ShortName { get; set; }
public string Name { get; set; }
public int Regon { get; set; }
}
接下来要从您的查询中获取数据,试试这个:
CustomClass item = new CustomClass();
String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @id";
using (var conn = new OleDbConnection("connectionString"))
{
conn.Open();
using (var cmd = new OleDbCommand(sql_user, conn))
{
cmd.Parameters.Add("@id", OleDbType.Integer).Value = Request.QueryString["a"];
cmd.Prepare();
var reader = cmd.ExecuteReader();
if(reader.Read())
{
item.Id = (int)reader["ID"];
item.ShortName = reader["SHORT_NAME"].ToString();
item.Name= reader["NAME"].ToString();
item.Regon = (int)reader["REGON"];
}
conn.Close();
}
}
编辑: 如果你不想使用它,你需要改变 taht 行:
OleDbDataReader customer = sql_s.ExecuteReader();
至:
OleDbDataReader customer;
var dr = sql_s.ExecuteReader();
if (dr.Read())
customer = dr;
试试这个,然后写出来