sql server-C#-我试图将我的数据库以表单形式连接到DataGridView,但有';这是个错误
本文关键字:但有 DataGridView 错误 连接 server-C#- 我的 表单 数据库 sql | 更新日期: 2023-09-27 18:00:45
我是C#编程的新手,一直在学习本教程:
https://www.youtube.com/watch?v=vQ2QjRr3toM
我使用的是C#和SQL Server
我正试图让我的[用户]表显示在数据网格视图上。
表单代码:
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
如何解决此错误?我已经按照教程中的步骤进行了操作(
请帮忙,因为这是我的论文,我只有有限的时间:'(
仔细检查连接字符串。
您会注意到,在InnerException中,有一条消息说明
初始化字符串的格式不符合规范从索引0开始。
导致此异常的跟踪使我相信连接字符串在这里出错。
可能存在拼写错误或报价问题。
这是因为在构造函数的第一行就有一个硬编码字符串。您需要从配置文件中获取值,而不是传入配置项的字符串值。
static private SqlCeConnection connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Software.Properties.Settings.DatabaseConnectionString"].ConnectionString);
您也可以查看select语句,关键字select拼写错误。
从连接字符串来看,您似乎没有使用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()););