我是C#编程新手并且一直关注本教程:
https://www.youtube.com/watch?v=vQ2QjRr3toM
我正在使用C#和SQL Server 我试图让我的[User]表显示在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
如何解决此错误?我已经按照教程中的步骤操作了。 :(
请帮助,因为这是我的论文,我的时间有限:&#39;(
答案 0 :(得分:0)
仔细检查您的连接字符串。
你会注意到在InnerException中,有一条消息说明
初始化字符串的格式不符合规范 从索引0开始。
导致此异常的跟踪使我相信连接字符串在这里有问题。
错误可能存在拼写错误或报价问题。
答案 1 :(得分:0)
这是因为你在第一行的构造函数中有一个硬编码字符串。您需要从配置文件中获取值,而不是传入配置条目的字符串值。
static private SqlCeConnection connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Software.Properties.Settings.DatabaseConnectionString"].ConnectionString);
您也可以查看您的select语句,关键字select is mispelled。
答案 2 :(得分: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();
}
}
}
*您的查询中的&#39; SELECT`拼写错误。
编辑:
不要对你的连接字符串进行硬编码。
static private SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Software.Properties.Settings.DatabaseConnectionString"].ToString()););