winfrom项目中配置文件
App.config文件如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connectionStringsName" connectionString="Data Source=06C0358E3B284CD\SQLEXPRESS;Initial Catalog=Shoponelinemanagement;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
类文件如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace Winter_vacation_homework
{
class Sqlconnectionfactory
{
private static string str;
private static SqlConnection con;
private Sqlconnectionfactory()
{
str = System.Configuration.ConfigurationManager.AppSettings["connectionStringsName"];
}
public static SqlConnection GetSqlConnection()
{
con = new SqlConnection(str);
con.Open();
return con;
}
public static void NonQuery(string sql)
{
SqlCommand cmd = new SqlCommand(sql, GetSqlConnection());
cmd.ExecuteNonQuery();
}
public static SqlDataReader Select(string sql)
{
SqlCommand cmd = new SqlCommand(sql, GetSqlConnection());
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}
public static void Close()
{
con.Close();
}
}
}调试出错
如以上代码中con = new SqlConnection(str);str换成具体的字符串="Data Source=06C0358E3B284CD\SQLEXPRESS;Initial Catalog=Shoponelinemanagement;User ID=sa;Password=sa"则运行成功
请问配置文件错在哪里,请大虾们指教,小弟感激不尽
App.config文件如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connectionStringsName" connectionString="Data Source=06C0358E3B284CD\SQLEXPRESS;Initial Catalog=Shoponelinemanagement;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
类文件如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace Winter_vacation_homework
{
class Sqlconnectionfactory
{
private static string str;
private static SqlConnection con;
private Sqlconnectionfactory()
{
str = System.Configuration.ConfigurationManager.AppSettings["connectionStringsName"];
}
public static SqlConnection GetSqlConnection()
{
con = new SqlConnection(str);
con.Open();
return con;
}
public static void NonQuery(string sql)
{
SqlCommand cmd = new SqlCommand(sql, GetSqlConnection());
cmd.ExecuteNonQuery();
}
public static SqlDataReader Select(string sql)
{
SqlCommand cmd = new SqlCommand(sql, GetSqlConnection());
SqlDataReader dr=cmd.ExecuteReader();
return dr;
}
public static void Close()
{
con.Close();
}
}
}调试出错
如以上代码中con = new SqlConnection(str);str换成具体的字符串="Data Source=06C0358E3B284CD\SQLEXPRESS;Initial Catalog=Shoponelinemanagement;User ID=sa;Password=sa"则运行成功
请问配置文件错在哪里,请大虾们指教,小弟感激不尽
connectionString=@"Data Source=06C0358E3B284CD\SQLEXPRESS;Initial Catalog=Shoponelinemanagement;User ID=sa;Password=sa" providerName="System.Data.SqlClient"
试下吧,我的分析是字符中的斜杠被转义了,你可以在“con = new SqlConnection(str);
”这里设个断点,然后单步走一下看看str里的内容。
看得到串与Config里的串对比一下!
先找问题的纠结点!
我一直都是这个方法的做的 Srt都是放在模块里的 应该没什么问题 你在检查检查细节
或使用Properties.Settings.Default.NorthwindConnectionString;
还是不行,至于Properties.Settings.Default.NorthwindConnectionString;
这句小弟就不理解了,而且也不行,希望大家能给小弟解惑,这个问题好像很严重啊,大家请再帮帮小弟啊