我创建了一个在Visual Studio中运行良好的软件。 但是当我在任何计算机上部署它时,它在尝试连接数据库时出现以下错误: -
************** Exception Text **************
System.InvalidOperationException: The ConnectionString property has not been initialized.
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at DentalGood.frmLogin.startLogin() in E:\PRGS\DentalGood\DentalGood\frmLogin.cs:line 28
at KryptonForm.frmMain.btnLogin_Click(Object sender, EventArgs e) in E:\PRGS\DentalGood\DentalGood\frmMain.cs:line 216
at ComponentFactory.Krypton.Ribbon.KryptonRibbonGroupButton.OnClick(EventHandler finishDelegate)
at ComponentFactory.Krypton.Ribbon.KryptonRibbonGroupButton.PerformClick(EventHandler finishDelegate)
at a5.d(Object A_0, EventArgs A_1)
at eq.b(Object A_0, EventArgs A_1)
at ek.b(EventArgs A_0)
at ek.a(Control A_0, Point A_1, MouseButtons A_2)
at ComponentFactory.Krypton.Toolkit.ToolTipController.MouseUp(Control c, Point pt, MouseButtons button)
at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
at ComponentFactory.Krypton.Toolkit.ViewManager.MouseUp(MouseEventArgs e, Point rawPt)
at ComponentFactory.Krypton.Toolkit.ViewControl.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at ComponentFactory.Krypton.Toolkit.ViewControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.6400 (Win8RTMGDR.050727-6400)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
已初始化program.cs文件中的ConnectionString。 它将连接字符串存储在文件中,并在需要连接时加载它。 有什么帮助吗?
答案 0 :(得分:0)
首先,从字符串中排除服务器的名称。改为键入点(。)。这意味着你连接到localhost上安装的sql server。它将为您提供测试应用程序连接字符串的机会。
其次,您不能将连接字符串存储在文件中,而是在程序代码中生成它。你可以输入类似的东西(不在Program.cs中):
public Form2()
{
InitializeComponent();
conn = "";
}
private void btnOK_Click(object sender, EventArgs e)
{
conn = CreateConn(conn, comboBox1.Text);
SqlConnection connect = new SqlConnection(conn);
MessageBox.Show(connect.ConnectionString.ToString());
}
public static string CreateConn(string connectString, string param)
{
connectString = String.Format("Data Source={0};Initial Catalog=db_Raspisanie;Integrated Security=True",
param);
return connectString;
}
UPD2。 在ComboBox1属性SelectedText中,您可以编写(本地),这将为用户提供每次都不输入服务器名称的机会,如果他在数据库所在的本地服务器上工作存储。
这是一个非常简单的方法,MessabeBox.Show
它会显示你得到的连接字符串。
<强> UPD。强>
如果您需要将其存储在文件中(我为此目的使用.txt),您可以使用以下代码:
/*Code of Form2, which is Login like*/
public static string conn;
private void Form2_Load(object sender, EventArgs e)
{
}
private void btnOK_Click(object sender, EventArgs e)
{
conn = CreateConn(conn);
SqlConnection connect = new SqlConnection(conn);
//MessageBox.Show(connect.ConnectionString.ToString());
Form1 fr = new Form1();
fr.ShowDialog();
}
public static string CreateConn(string connectString)
{
string fileName = Application.StartupPath + "\\string.txt";
FileStream fs = new FileStream(fileName, FileMode.Open);
StreamReader strem = new StreamReader(fs);
conn = strem.ReadLine();
connectString = conn;
strem.Close(); fs.Close();
return connectString;
}
/*Code of Form1 which is main form*/
public Form1()
{
InitializeComponent();
string param = "";
param = Form2.conn;
con = new SqlConnection(param);
}
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Select * from Table1", con);
ds = new DataSet();
da.Fill(ds, "Table1");
dataGridView1.DataSource = ds.Tables["Table1"];
}
.txt文件的内容与您所写的相同:
Server=.;database=db_Raspisanie;trusted_connection=true
似乎我根据需要包含了所有内容。如果它给出例外,请写,我测试了,但可能是。