2015-07-01 15 views
0

我是新的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 

我該如何解決這個問題?我已經按照教程中的步驟操作。 :(
請幫助,因爲這是我的論文,我只有有限的時間。「(

+0

您使用的是SQL Server精簡版嗎? –

+0

我不確定。我怎麼知道? –

回答

0

仔細檢查您的連接字符串

你會在InnerException注意到,有一個消息,說明

格式的初始化字符串的不符合規格 開始在索引0

導致這種異常的痕跡讓我相信連接字符串在這裏是錯誤的。

可能有錯誤或報價問題。

0

這是因爲在第一行中您的構造函數中有一個硬編碼的字符串。您需要從配置文件中獲取值,而不是傳遞配置條目的字符串值。

static private SqlCeConnection connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Software.Properties.Settings.DatabaseConnectionString"].ConnectionString); 

您可能還會看看您的select語句,關鍵字select拼寫錯誤。

+0

謝謝!我已經嘗試了你所說的內容,並且糾正了我的Select語句,但仍然在InnerException消息中出現錯誤:「不支持關鍵字:'attachdbfilename'。」 –

+0

這是因爲你的配置文件中仍然有亂碼。 –

0

從連接字符串,它看起來像你沒有使用SQL Server CE。這就是說你應該使用SqlConnection而不是SqlCeConnection

static class SQLFunctions 
{ 
    static private SqlConnection connection = new SqlConnection("Software.Properties.Settings.DatabaseConnectionString"); 

    static public void Refresh(DataGridView _dataGridView) 
    { 
     try 
     { 
      connection.Open(); 
      SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM [User]", connection); 
      DataTable dataTable = new DataTable(); 
      dataAdapter.Fill(dataTable); 
      _dataGridView.DataSource = dataTable; 
     } 
     catch(SqlException exception) 
     { 
      MessageBox.Show(exception.ToString()); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
} 

*同樣在您的查詢中'SELECT'拼寫錯誤。

編輯:

不要硬編碼您的連接字符串。

static private SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Software.Properties.Settings.DatabaseConnectionString"].ToString());); 
+0

謝謝你,我已經試過並糾正了我的Select語句,但我仍然在InnerException消息中出現錯誤,因爲「初始化字符串的格式不符合從索引0開始的規範。」 –

相關問題