[英]SQL Server Views with asp.net
I have created a view called view_SelectAll_Student
in SQL Server which retrieves all columns from single table name 我已经在SQL Server中创建了一个名为
view_SelectAll_Student
的视图,该视图从单个表名称中检索所有列
And I have a function that returns dataset or datatable using dataadapter somehow I get this error: 我有一个函数可以使用dataadapter返回数据集或数据表,但我却收到此错误:
The request for procedure 'view_SellectAll_Student' failed because 'view_SellectAll_Student' is a view object.
过程'view_SellectAll_Student'的请求失败,因为'view_SellectAll_Student'是一个视图对象。
Code: 码:
public DataTable ViewStudentAll()
{
cons.Open();
DataTable dt = new DataTable();
cmd = new SqlCommand("view_SellectAll_Student", cons);
cmd.Connection = cons;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
}
Views still need to be queried. 仍然需要查询视图。 What you have here is just the view name..
您所拥有的只是视图名称。
So change this: 所以改变这个:
cmd = new SqlCommand("view_SellectAll_Student",cons);
to this: 对此:
cmd = new SqlCommand("SELECT put, columns, here FROM view_SellectAll_Student",cons);
Make sure you put the columns of the view there (or an asterisk.. if you're that way inclined). 确保在此处放置视图的列(或倾斜的星号)。
Write it like this. 这样写。 If its a view you should SELECT it otherwise you wont get it.
如果是视图,则应选择它,否则将无法得到它。
cmd = new SqlCommand("SELECT * FROM view_SellectAll_Student",cons);
cmd.Connection = cons;
//cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
TIP: While using a DataAdapter
there is no need of con.Open()
or con.Close()
statement. 提示:使用
DataAdapter
,不需要con.Open()
或con.Close()
语句。 DataAdapter
itself will open and close it. DataAdapter
本身将打开和关闭它。
The SqlDataAdapter accepts as first argument an SqlCommand which can be a Select statement or stored procedure.
SqlDataAdapter接受SqlCommand作为第一个参数,该命令可以是Select语句或存储过程。
In this case you can replace "view_SellectAll_Student" with 在这种情况下,您可以将“ view_SellectAll_Student”替换为
"Select * from view_SellectAll_Student"
cons.Open();
DataTable dt = new DataTable();
cmd = new SqlCommand("select * view_SellectAll_Student",cons);
cmd.Connection = cons;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
cons.Close();
adp.Dispose();
return dt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.