在 C# 中连接和查询数据库以获取一组记录的最简单方法是什么?
问问题
30179 次
8 回答
35
@Goyuix - 这对于从记忆中编写的东西来说非常棒。在这里测试它 - 发现连接没有打开。否则非常好。
using System.Data.OleDb;
...
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = "Provider=sqloledb;Data Source=yourServername\\yourInstance;Initial Catalog=databaseName;Integrated Security=SSPI;";
using (OleDbCommand cmd = new OleDbCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Select * from yourTable";
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine(dr["columnName"]);
}
}
}
}
于 2008-08-04T01:31:31.157 回答
17
因为我在这台笔记本电脑上没有代码,所以非常粗略地从内存中获取:
using (OleDBConnection conn = new OleDbConnection())
{
conn.ConnectionString = "Whatever connection string";
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = conn;
cmd.CommandText = "Select * from CoolTable";
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
// do something like Console.WriteLine(dr["column name"] as String);
}
}
}
}
于 2008-08-04T01:07:14.760 回答
12
这绝对是一个好方法。但是,如果您碰巧使用的是支持 LINQ to SQL 的数据库,那会更有趣。它看起来像这样:
MyDB db = new MyDB("Data Source=...");
var q = from db.MyTable
select c;
foreach (var c in q)
Console.WriteLine(c.MyField.ToString());
于 2008-08-04T04:45:05.367 回答
7
这是另一种方式(DataReader 比这个更快):
string s = "";
SqlConnection conn = new SqlConnection("Server=192.168.1.1;Database=master;Connect Timeout=30;User ID=foobar;Password=raboof;");
SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 5 name, dbid FROM sysdatabases", conn);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
s += dt.Rows[i]["name"].ToString() + " -- " + dt.Rows[i]["dbid"].ToString() + "\n";
}
MessageBox.Show(s);
于 2008-08-05T14:17:22.530 回答
4
如果您正在查询 SQL Server 数据库(版本 7 及更高版本),则应将 OleDb 类替换为System.Data.SqlClient命名空间(SqlConnection、SqlCommand和SqlDataReader)中的相应类,因为这些类已针对 SQL Server 进行了优化。
另一件需要注意的事情是,您应该“永远不要”全选,因为如果您在此表中添加或删除列,这可能会导致稍后出现意外结果。
于 2008-08-21T14:19:29.530 回答
4
如果您打算读取大量列或记录,那么缓存序号并访问强类型方法也是值得的,例如
using (DbDataReader dr = cmd.ExecuteReader()) {
if (dr.Read()) {
int idxColumnName = dr.GetOrdinal("columnName");
int idxSomethingElse = dr.GetOrdinal("somethingElse");
do {
Console.WriteLine(dr.GetString(idxColumnName));
Console.WriteLine(dr.GetInt32(idxSomethingElse));
} while (dr.Read());
}
}
于 2008-09-15T19:07:17.307 回答
1
我想,你可以试试实体框架。
using (SchoolDBEntities ctx = new SchoolDBEntities())
{
IList<Course> courseList = ctx.GetCoursesByStudentId(1).ToList<Course>();
//do something with courselist here
}
于 2012-09-05T19:11:25.733 回答
0
向图书馆收费
using MySql.Data.MySqlClient;
这是连接:
public static MySqlConnection obtenerconexion()
{
string server = "Server";
string database = "Name_Database";
string Uid = "User";
string pwd = "Password";
MySqlConnection conect = new MySqlConnection("server = " + server + ";" + "database =" + database + ";" + "Uid =" + Uid + ";" + "pwd=" + pwd + ";");
try
{
conect.Open();
return conect;
}
catch (Exception)
{
MessageBox.Show("Error. Ask the administrator", "An error has occurred while trying to connect to the system", MessageBoxButtons.OK, MessageBoxIcon.Error);
return conect;
}
}
于 2019-05-17T03:40:00.830 回答