我的以下代码工作正常,但是如何在其中添加where子句。
一直在尝试以下字符串:
sql =“从[设备]中选择[ip_address],[name],[model],[mac_address],[operating_system],[current_user],其中[ip_address] = @ 192.168.0.56”;
using System;
using System.Data.SQLite;
using System.IO;
namespace SQLiteSamples
{
class Program
{
// Holds our connection with the database
SQLiteConnection m_dbConnection;
public string Info { get; private set; }
static void Main(string[] args)
{
Program p = new Program();
}
public Program()
{
connectToDatabase();
printResult();
}
// Creates a read only connection to spiceworks database file.
void connectToDatabase()
{
//m_dbConnection = new SQLiteConnection(@"Data Source=\\spiceworks\db\spiceworks_prod.db;Version=3;Read Only=True");
m_dbConnection = new SQLiteConnection(@"Data Source=C:\Temp\ZipSampleExtract\db\spiceworks_prod.db;Version=3;Read Only=True");
m_dbConnection.Open();
}
// Writes the output from spiceworks table devices.
void printResult()
{
string sql = "select [ip_address], [name], [model], [mac_address], [operating_system], [current_user]from [devices]";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine("ip_address: " + reader["ip_address"] + " " + reader["name"] + " " + reader["model"] + " " + reader["mac_address"]+" " + reader["operating_system"] + " " + reader["current_user"]);
Info = Convert.ToString(Console.ReadLine());
File.WriteAllText("E:\\johnb.txt", Info);
}
}
}
最佳答案
SQL中的字符串文字用单引号引起来:
string sql = "select [ip_address], [name], [model], [mac_address], [operating_system], [current_user] from [devices] where [ip_address] = '192.168.0.56'";
关于c# - 带C#和where子句的SQLite,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50989270/