【问题标题】:App.config acts different than the regular wayApp.config 的行为与常规方式不同
【发布时间】:2013-07-08 06:35:17
【问题描述】:

我的连接字符串以前是这样的:

SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\billingSystem\Store.mdf;Integrated Security=True;User Instance=True");

所以我可以(插入、更新、删除):

    SqlCommand cmd = new SqlCommand("SQLSTATEMENT", cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();

我在 youtube 上看过一个名为“部署 C# 项目”的教程,当时他使用

System.Configuration;

app.config 文件,所以我按照教程完全相同的方式进行操作。

我在运行时(插入、更新、删除)之后一切正常,直到我关闭应用程序,就像我什么都没做一样,我插入的所有数据都消失了!。

我需要在运行时将更改保存到表中。

一些信息:Winforms 应用程序,来自我的代码示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="SchoolProjectConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SchoolDB.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

namespace SchoolProject
{
    public partial class Main : Form
    {
        SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SchoolProjectConnectionString"].ToString());
        public Main()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string myValue = Interaction.InputBox("Enter ID", "ID", "", 100, 100);
            string myValue0 = Interaction.InputBox("Enter Name", "Name", "", 100, 100);
            int X = Convert.ToInt32(myValue);

            SqlCommand cmd = new SqlCommand("Insert into Student(ID, Student) Values ('" + X + "','" + myValue0 + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string myValue3 = Interaction.InputBox("Enter ID", "ID", "", 100, 100);
            int X = Convert.ToInt32(myValue3);

            SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE id = '" + X + "'", cn);
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    MessageBox.Show(reader["Student"].ToString());
                }
            }
            cn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Report R = new Report();
            R.Show();
        }
    }
}

【问题讨论】:

  • 连接字符串或 app.config 在 Visual Studio 调试器中的“行为”不太可能与在运行时不同。除非您明确指定使用XDT transformations。请将您的 app.config 的内容添加到问题中;如果您的项目没有出现任何错误,但它似乎确实有效,那么您可能正在与不同的数据库通信?
  • 检查您的项目,Db 在 Solution Expl 中是否可见,是否有 CopyLocal=true ?
  • 是可见的,如何知道它是否有 CopyLocal=true ?

标签: c# .net sql-server app-config


【解决方案1】:

整个 User Instance 和 AttachDbFileName= 方法是有缺陷的 - 充其量!在 Visual Studio 中运行您的应用程序时,它将复制 .mdf 文件(从您的 App_Data 目录到输出目录 - 通常是 .\bin\debug - 您的应用程序运行的位置)并且很可能 ,您的 INSERT 工作正常 - 但您最终只是查看了错误的 .mdf 文件

如果您想坚持这种方法,请尝试在 myConnection.Close() 调用上设置断点 - 然后使用 SQL Server Mgmt Studio Express 检查 .mdf 文件 - 我几乎可以肯定您的数据在那里。

在我看来,真正的解决方案

  1. 安装 SQL Server Express(反正你已经完成了)

  2. 安装 SQL Server Management Studio Express

  3. SSMS Express中创建你的数据库,给它一个逻辑名称(例如Store

  4. 使用它的逻辑数据库名称(在服务器上创建它时给出)连接到它——不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:

    Data Source=.\\SQLEXPRESS;Database=Store;Integrated Security=True
    

    其他一切都一模一样和以前一样......

另外:您应该阅读 SQL 注入 并将您的代码更改为 避免 - 这是应用程序的第一大安全风险。您应该在 ADO.NET 中使用参数化查询 - 这些是安全的,而且速度也更快!

【讨论】:

  • 感谢回答,是的,.\bin\debug 中的 .mdf 似乎包含所有数据,但我无法获取或查看它(请参阅我的代码中的 button2),第二个方法我实际上不喜欢安装 SSMS,因为它要求我在客户端实际呈现,而不是我只想发送包。
猜你喜欢
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多