连接SQL Server数据库时出现系统无效操作符错误

本文关键字:系统 无效 操作符 错误 SQL Server 数据库 连接 | 更新日期: 2023-09-27 17:51:04

这是我的程序。我是c#的新手,也是编程的新手。我得到一个错误,而运行这个程序。我在这里要做的是从App.config加载连接字符串,然后连接到数据库,从数据表中读取,并将几列导出到Excel作为。csv:

class Program
{       
    public static string ConnectionString = ConfigurationManager.AppSettings["DbSource"];
    static void Main(string[] args)
    { 
        GetCpudata();  
    }
    //Export to csv ORACLE Test CPU Data
    public static void GetCpudata()
    {
        StringBuilder sb = new StringBuilder();
        using (SqlConnection con1 = new SqlConnection(ConnectionString))
        {       
            con1.Open();   ////ERROR HERE
            Console.WriteLine("Opening db");
            SqlDataAdapter da = new SqlDataAdapter(" select * from ComplianceComputer_MT", con1);
            DataSet sourcedata = new DataSet();
            da.Fill(sourcedata);
            sourcedata.Tables[0].TableName = "Test";
            foreach (DataRow row in sourcedata.Tables["Test"].Rows)
            {
                sb.Append(row["ComputerName"].ToString()); 
                sb.Append(row["[MaxClockSpeed]"].ToString()); 
                sb.Append(row["[Manufacturer]"].ToString()); 
                sb.Append(row["[ModelNo]"].ToString()); 
                sb.Append(row["[NumberOfCores]"].ToString()); 
                sb.Append("'r'n");
            }
        }
        StreamWriter file = new StreamWriter(@"C:'StudyC'TestData'Exported Data'Test.csv");
        file.WriteLine(sb.ToString());
        file.Close();
    }
}

App.config中的Dbsource .

My App Config settings:

<connectionStrings>
    <add name="DbSource" connectionString="Data Source=192.168.10.109'SAMPLE;Initial Catalog=FlexNet;Integrated Security=True;User ID=test;Password=test;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" />
</connectionStrings>

另外,我希望在excel中有更多的列,当数据库表中没有相应的列可用时,需要填写为null。

有什么办法吗?

连接SQL Server数据库时出现系统无效操作符错误

这会得到正确的连接字符串:

public static string ConnectionString = ConfigurationManager.ConnectionStrings["DbSource"].ConnectionString;