首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有c#和where子句的Sqlite

带有c#和where子句的Sqlite
EN

Stack Overflow用户
提问于 2018-06-22 13:39:17
回答 2查看 4.1K关注 0票数 2

下面的代码运行良好,但是如何在其中添加where子句。

一直在尝试以下字符串: sql =“选择ip_address、名称、模型、mac_address、operating_system、current_userfrom设备,其中ip_address = @192.168.0.56";

代码语言:javascript
复制
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);

       }

    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-22 13:42:37

SQL中的字符串文字用单引号表示:

代码语言:javascript
复制
string sql = "select [ip_address], [name], [model], [mac_address], [operating_system], [current_user] from [devices] where [ip_address] = '192.168.0.56'";
票数 1
EN

Stack Overflow用户

发布于 2018-06-22 13:56:10

您将希望在最终项目中使用参数来防止SQL注入。

代码语言:javascript
复制
    string sql = "select [ip_address], [name], [model], [mac_address], [operating_system], [current_user]from [devices] where [ip_address] = @ip_address";
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    command.Parameters.Add(new SqlParameter("@ip_address", ipAddressValue));

    SQLiteDataReader reader = command.ExecuteReader();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50989270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档