C#连接Sql Server时出现未经处理异常:System.ArgumentException:“不支持关键字: “integrated

img


img


我刚开始学习C#,对于这些还在摸索,不太了解。希望有厉害的小伙伴可以指点一下!!

具体什么问题我不清楚,我没用过你这种方式连接sqlserver,你可以试试我的这种方式连接sqlserver

之前研究C#连接MySQL时顺便学习了一下C#连接SQL server,链接在这里,代码在下面!

https://blog.csdn.net/cpp_learner/article/details/108296318

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 连接数据库 {

    class Program {
        static void Main(string[] args) {

            // 附送SQL Server 的数据库连接方式,拿走不谢!
            // 需要包含头文件:using System.Data;  // 表的命名空间
            // using System.Data.SqlClient;    // 和SQL相关的命名空间
            #region SQL Server 连接数据库的方式
            string id = "0";

            Console.WriteLine("请输入需要查询的id:");
            id = Console.ReadLine();




            // 声明一个连接数据库的对象
            SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();

            // 设置连接数据库的IP地址
            scsb.DataSource = "127.0.0.1";

            // 设置登录数据库的账号
            scsb.UserID = "root";

            // 设置登录数据库的密码
            scsb.Password = "yang";

            // 设置连接的数据库名
            scsb.InitialCatalog = "school";



            // 创建连接
            SqlConnection conn = new SqlConnection(scsb.ToString());

            // 打开连接
            conn.Open();

            // 创建要执行的SQL语句
            String sqlSelect = "select * from class1 where id = " + id + ";";

            // 创建用于实现SQL语句的对象
            SqlCommand comm = new SqlCommand(sqlSelect, conn);  // 参数一:SQL语句字符串 参数二:已经打开的数据库连接对象
            // 执行comm对象,接收查询到的SQL结果
            SqlDataReader sdr = comm.ExecuteReader();


            // 读取数据
            while (sdr.Read()) {
                Console.WriteLine(sdr["Name"]);
            }

            #endregion


            Console.WriteLine();
            Console.ReadKey();
        }
    }
}


是integrated security,把th改为ty

img