C# App.config的行为与常规方式不同
我的连接字符串以前是这样的:C# App.config的行为与常规方式不同,c#,.net,sql-server,app-config,C#,.net,Sql Server,App Config,我的连接字符串以前是这样的: 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);
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上看过一个名为DeployC Project的教程,当时他使用
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();
}
}
}
整个用户实例和AttachDbFileName=方法都有缺陷-最多!在Visual Studio中运行应用程序时,它将围绕.mdf文件从应用程序的数据目录复制到输出目录-通常是。\bin\debug-应用程序运行的地方,很可能插入工作正常-但最终您看到的是错误的.mdf文件
如果您想坚持使用这种方法,那么请尝试在myConnection.Close调用上放置一个断点-然后使用SQL Server Mgmt Studio Express检查.mdf文件-我几乎可以肯定您的数据在那里
在我看来,真正的解决办法是
安装SQL Server Express,您已经完成了安装
安装SQL Server Management Studio Express
在SSMS Express中创建数据库,并为其指定一个逻辑名称,例如存储
使用在服务器上创建时提供的逻辑数据库名称连接到它,并且不要混淆物理数据库文件和用户实例。在这种情况下,您的连接字符串类似于:
Data Source=.\\SQLEXPRESS;Database=Store;Integrated Security=True
其他一切都和以前一样
另外:你应该仔细阅读SQL注入并更改代码以避免这种情况——这是应用程序的首要安全风险。您应该在ADO.NET中使用参数化查询-这些查询是安全的,而且速度更快 连接字符串或app.config在Visual Studio调试器中的行为不太可能与在运行时中的行为不同。除非您明确指定使用。请将app.config的内容添加到问题中;如果您的项目中没有出现任何错误,但它似乎正常工作,您可能正在与其他数据库交谈?检查您的项目,Db在解决方案EXP中是否可见,并且它是否具有CopyLocal=true?是的,它是可见的,如何知道它是否具有CopyLocal=true?Thanq作为答案,是的。\bin\debug中的.mdf似乎包含所有数据,但是我无法获取或查看它,请在代码中查看按钮2,第二种方法实际上我不喜欢安装SSMS,因为它需要我在客户端进行物理演示,而我只想发送包。