如何使用 C# 读取列和行? [英] How to read columns and rows with C#?
本文介绍了如何使用 C# 读取列和行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我需要一个代码来读取 C# 的列和行.
Hello I'm in need of a code to read columns and rows for C#.
我已经走到这一步了:
obj.MysqlQUERY("SELECT * FROM `players` WHERE name = "+name+";"); // my query function
感谢帮助;]
推荐答案
这是我经常与 MySql 一起使用的标准代码块.请注意,您必须使用此处提供的 MySql 连接器.
Here is a standard block of code that I use with MySql a lot. Note that you must be using the MySql connector available here.
string myName = "Foo Bar";
using (MySqlConnection conn = new MySqlConnection("your connection string here"))
{
using (MySqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = @"SELECT * FROM players WHERE name = ?Name;";
cmd.Parameters.AddWithValue("Name", myName);
MySqlDataReader Reader = cmd.ExecuteReader();
if (!Reader.HasRows) return;
while (Reader.Read())
{
Console.WriteLine(GetDBString("column1", Reader);
Console.WriteLine(GetDBString("column2", Reader);
}
Reader.Close();
conn.Close();
}
}
private string GetDBString(string SqlFieldName, MySqlDataReader Reader)
{
return Reader[SqlFieldName].Equals(DBNull.Value) ? String.Empty : Reader.GetString(SqlFieldName);
}
请注意,如果数据库值为空,我将使用一种方法返回特定值.您可以发挥创意并提供各种返回值或合并可为空类型等.
Note that I am using a method to return a specific value if the database value is null. You can get creative and provide various return values or incorporate nullable types, etc.
这篇关于如何使用 C# 读取列和行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文