gpt4 book ai didi

c# - 我的 c# 代码在访问 sql server 时抛出异常

转载 作者:行者123 更新时间:2023-11-30 17:56:24 28 4
gpt4 key购买 nike

我是 c# 和 sql 的新手我正在创建一个简单的学生数据库我添加了在 sql server 2008 中创建的数据库。现在我有一个表格,其中给出了输入,我有一个按钮插入将数据插入数据库。但是当我点击按钮时,我得到了一个异常(exception)。

这是我的 App.config 文件

`<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

</startup>
<configSections>
<connectionStrings>
<appSettings>
<add name="ConString" connectionString="Data Source=ARAVIND-HP\SQLEXPRESS; Initial Catalog=test;Integrated Security=True" providerName="System.Data.sqlClient"/>
</appSettings>
</connectionStrings>

`

我使用的表格如下所示

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 test
{
public partial class frmNewStudent : Form
{

public frmNewStudent()
{
InitializeComponent();
}

private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}

private void frmNewStudent_Load(object sender, EventArgs e)
{

}
private void btnInsert_Click(object sender, EventArgs e)
{
DB_Access access = new DB_Access();
access.add_student(txtRegNo.Text,txtFName.Text,txtLName.Text,txtPhone.Text);
MessageBox.Show("Data added successfully");
}
}
}

现在我有两个类 1.DB_access 2.DB_Connections

DB_access的代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace test
{
class DB_Access
{
public SqlConnection conn;
public DB_Access()
{
conn = DB_Connection.GetConnection();
}

public void add_student(string regno, string fname, string lname, string phone)
{
if(conn.State.ToString()=="Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "insert into student values('"+ regno +"','"+ fname +"','"+ lname +"','"+ phone +"')";
newCmd.ExecuteNonQuery();
}
}
}

DB_Connections 的代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace test
{
class DB_Connection
{
public static SqlConnection NewCon;

public static string ConStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

public static SqlConnection GetConnection()
{
NewCon = new SqlConnection(ConStr);
return NewCon;
}


}
}

当我运行它并单击插入按钮时,出现以下异常:

test.exe 中发生类型为“System.TypeInitializationException”的未处理异常

附加信息:“test.DB_Connection”的类型初始值设定项引发异常。

和“conn = DB_Connection.GetConnection();”这一行突出显示

我找不到错误。请帮我解决这个问题

最佳答案

System.TypeInitializationException 表示程序无法创建类型 DB_Connection,更准确地说无法初始化静态字段 ConStr

connectionStrings 部分的使用不正确。看http://msdn.microsoft.com/en-us/library/ms254494.aspx .

关于c# - 我的 c# 代码在访问 sql server 时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com