【发布时间】: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