ASPX SQL选择命令 [英] ASPX SQL Select Command
本文介绍了ASPX SQL选择命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
在aspx.cs中,有没有一种方法可以从数据库中读取数据?我似乎与C#完全不同.
我需要使用select命令从数据库中读取数据,然后将我刚刚读取的值放在一个变量中,只是一个简单的SQL Select命令,我想我可能缺少某些语法.
问候.
谢谢.
Hello,
Is there a way, in aspx.cs, to read data from a database? I doesnt seem to be exactly the same as in C#.
I need to use the select command to read from the database and put the value i just read in a variable, just a simple SQL Select command, i think i might be missing some syntax.
Regards.
Thank You.
推荐答案
如果您使用的是C#,那么代码将几乎相同:只需使用参数化查询即可.
If you are using C#, then the code is going to be pretty much the same: just use a parametrized query.
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable WHERE Id=@ID", con))
{
com.Parameters.AddWithValue("@ID", id);
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["iD"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", iD, desc);
}
}
}
}
或:
Or:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
{
da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
DataTable dt = new DataTable();
da.Fill(dt);
myDataGridView.DataSource = dt;
}
}
还是有您尝试做的更具体的尝试失败了?
Or is there something more specific your are trying to do that is failing?
看到它可能对您有帮助
see this it may help you
da = New SqlDataAdapter("select distinct( name) from sample", con)
ds = New DataSet()
da.Fill(ds)
CheckBox1.Text = ds.Tables(0).Rows(0)(0).ToString()
在此处阅读有关ADO.NET的信息,它将对您有所帮助.
看看这些:
MSDN:ADO.NET [ MSDN:使用ADO.NET访问数据 [ ^ ]
C#Station ADO.NET教程 [为初学者使用ADO.NET [简单的ADO.NET数据库读取,插入,更新和删除使用C#. [ ^ ]
Read about ADO.NET here and it will help you.
Look at these:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]
The C# Station ADO.NET Tutorial[^]
Using ADO.NET for beginners[^]
Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]
这篇关于ASPX SQL选择命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文