加入using System.Configuration;名称空间后软件提示错误的处理

软件在加入using System.Configuration;语句后, 在引入ConnectionStrings等参数时出现
The name 'ConfigurationManager' does not exist in the current context错误,其原因是仅手动引入未System.Configuration命名空间未在软件中添加System.Configuration.dll引用,添加Configuration的引用。

  1. Right-click on the References / Dependencies
  2. Choose Add Reference
  3. Find and add System.Configuration.Find and add System.Configuration.

在这里插入图片描述

参阅:https://stackoverflow.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current-context

以下是一个简单的DBHelper.cs类的代码示例,它可以连接到SQL Server数据库并执行一些基本的操作: ```csharp using System.Data; using System.Data.SqlClient; public class DBHelper { private static string connectionString = "Data Source=your_server_name_here;Initial Catalog=your_database_name_here;User ID=your_username_here;Password=your_password_here;"; public static DataTable ExecuteQuery(string sql) { DataTable table = new DataTable(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(table); } } return table; } public static int ExecuteNonQuery(string sql) { int rowsAffected = 0; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { rowsAffected = command.ExecuteNonQuery(); } } return rowsAffected; } public static object ExecuteScalar(string sql) { object result = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { result = command.ExecuteScalar(); } } return result; } } ``` 在上面的示例中,DBHelper类包含了三个方法:ExecuteQuery、ExecuteNonQuery和ExecuteScalar。这些方法分别用于执行查询语句、非查询语句和返回单个值的语句。方法中的SqlConnection和SqlCommand对象用于连接到数据库并执行SQL语句。注意,这个示例中没有使用System.Configuration命名空间中的配置文件,而是直接在代码中指定了连接字符串。如果需要使用配置文件,则可以添加一个静态构造函数或者一个初始化方法来读取配置文件中的连接字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值