我是新的C#編程,並一直跟着教程:
https://www.youtube.com/watch?v=vQ2QjRr3toMC# - 我嘗試我的數據庫連接的DataGridView的形式,但有一個錯誤
我使用C#和SQL Server。
我想讓我的[用戶]表顯示在datagridview上。
表單代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Software
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SQLFunctions.Refresh(this.dataGridView1);
}
}
}
SQLFunction類代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlServerCe;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
namespace Software
{
static class SQLFunctions
{
static private SqlCeConnection connection = new SqlCeConnection("Software.Properties.Settings.DatabaseConnectionString");
static public void Refresh(DataGridView _dataGridView)
{
try
{
connection.Open();
SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter("SELECCT * FROM [User]", connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
_dataGridView.DataSource = dataTable;
}
catch(SqlCeException exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
connection.Close();
}
}
}
}
的App.config
<connectionStrings>
<add name="Software.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
我的問題是,當我嘗試運行我的程序時,出現一個錯誤,說我的SQLFunctions類拋出了一個異常。
System.TypeInitializationException was unhandled
Message = "The type initializer for 'Software.SQLFunctions' threw an exception."
Source = Software
TypeName = Software.SQLFunctions
StackTrace:
at Software.SQLFunctions.Refresh(DataGridView _dataGridView)
at Software.Form1.Form1_Load(Object sender, EventArgs e) in c:\Users\misaru02\Documents\Visual Studio 2012\Projects\Thesis\Software\frmUserMgmt.cs:line 22
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
Message = "Format of the initialization string does not conform to specification starting at index 0."
Source = System.Data
StackTrace:
at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnectionStringBuilder..ctor(String connectionString)
at System.Data.SqlServerCe.SqlCeConnection.set_ConnectionString(String value)
at System.Data.SqlServerCe.SqlCeConnection..ctor(String connectionString)
at Software.SQLFunctions..cctor() in c:\Users\misaru02\Documents\Visual Studio 2012\Projects\Thesis\Software\SQLFunctions.cs:line 16
我該如何解決這個問題?我已經按照教程中的步驟操作。 :(
請幫助,因爲這是我的論文,我只有有限的時間。「(
您使用的是SQL Server精簡版嗎? –
我不確定。我怎麼知道? –