日期:2014-05-20  浏览次数:20781 次

连接数据库出问题
程序运行无错,但是执行结束后数据库中的数据并未改变
//查找数据库路径
  string dataDir = AppDomain.CurrentDomain.BaseDirectory;
  if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
  {
  dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;

  AppDomain.CurrentDomain.SetData("DataDirecrory", dataDir);
  }
  //从配置文件中读取字符串
  string ConnStr1 = ConfigurationManager.ConnectionStrings["ConnStr1"].ConnectionString;
  string ConnStr2 = ConfigurationManager.ConnectionStrings["ConnStr2"].ConnectionString;

  //string ConnStr = ConnStr1 + dataDir + ConnStr2;//换成这句话才起作用
  string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
  using (SqlConnection conn = new SqlConnection(connStr))
  {
  conn.Open();
  using (SqlCommand cmd = conn.CreateCommand())
  {
  cmd.CommandText = "delete from Numbers";
  MessageBox.Show(Convert.ToString( cmd.ExecuteNonQuery()));
  }
  }

//配置文件中的代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
  <add name="ConnStr"
  connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Phone.mdf;Integrated Security=True;User Instance=True"
  providerName="System.Data.SqlClient" />
  <add name="ConnStr1"
  connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="/>
  <add name ="ConnStr2"
  connectionString ="\Phone.mdf;
  Integrated Security=True;User Instance=True"/>
  </connectionStrings>
</configuration>




------解决方案--------------------
每天回帖即可获得10分可用分!
------解决方案--------------------
不懂,数据库既然打开了,为什么不关闭?
------解决方案--------------------
你发的地方不对