Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从c连接到SQL Server#_C#_Sql Server_Exception Handling - Fatal编程技术网

C# 从c连接到SQL Server#

C# 从c连接到SQL Server#,c#,sql-server,exception-handling,C#,Sql Server,Exception Handling,我之所以重新发布这个问题,是因为这里的很多人建议我重新发布错误消息和DB_连接的代码,错误出现在这个代码conn=DB_conction.GetConnection()上的DB_访问类中;,我正在用c sharp和ms sql sever 2008处理一个简单的数据库项目,但是我在编译程序时遇到了一个错误,它弹出了下面的消息 System.TypeInitializationException was unhandled Message=The type initializer for '

我之所以重新发布这个问题,是因为这里的很多人建议我重新发布错误消息和DB_连接的代码,错误出现在这个代码conn=DB_conction.GetConnection()上的DB_访问类中;,我正在用c sharp和ms sql sever 2008处理一个简单的数据库项目,但是我在编译程序时遇到了一个错误,它弹出了下面的消息

System.TypeInitializationException was unhandled

  Message=The type initializer for 'StudentsInformationSystem.DB_conection' threw an exception.
  Source=StudentsInformationSystem
  TypeName=StudentsInformationSystem.DB_conection
  StackTrace:
       at StudentsInformationSystem.DB_conection.GetConnection()
       at StudentsInformationSystem.DB_Access..ctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\DB_Access.cs:line 16
       at StudentsInformationSystem.frmNewStudent..ctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\frmNewStudent.cs:line 14
       at StudentsInformationSystem.Form1.btnAddNewStudent_Click(Object sender, EventArgs e) in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\Form1.cs:line 31
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.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)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at StudentsInformationSystem.Program.Main() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=StudentsInformationSystem
       StackTrace:
            at StudentsInformationSystem.DB_conection..cctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\DB_conection.cs:line 15
       InnerException:
这是我的DB_连接类

 class DB_conection
    {
        public static SqlConnection NewCon;
        public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

        public static SqlConnection GetConnection() {

            NewCon = new SqlConnection(constr);
            return NewCon;

        }

    }
这是我的DB_访问代码

namespace StudentsInformationSystem
{
    class DB_Access
    {
        SqlConnection conn;
        public DB_Access() {

            conn = DB_conection.GetConnection();

        }
        public void add_student(string regNo,string fname, string lname, string phoneNo){

            if (conn.State == ConnectionState.Closed) {

                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.CommandText = "insert into student values('" + regNo + "','" + fname + "','" + lname + "','" + phoneNo + "')";
            newCmd.ExecuteNonQuery();

        }
    }
}

DB\u连接
[sic]类的静态构造函数中有一个
NullReferenceException
。我唯一能找到的地方就是这一行(为了可读性增加了换行符):

特别是,
connectionstrings[string]
indexer属性返回
null
。通常,当无法找到名称指定的连接字符串(此处为
ConString
)时,它会这样做

确保您的应用程序配置文件(Visual Studio中名为
app.config
)包含
connectionString
元素中名为“consting”的连接字符串的定义

例如:



请注意,您的代码中还有一些其他问题,例如,至少在
SqlCommand
对象上的
Dispose
调用(或
使用
块)即将发生,等等。

您的
DB\u连接
[sic]类的静态构造函数中有一个
NullReferenceException
。我唯一能找到的地方就是这一行(为了可读性增加了换行符):

特别是,
connectionstrings[string]
indexer属性返回
null
。通常,当无法找到名称指定的连接字符串(此处为
ConString
)时,它会这样做

确保您的应用程序配置文件(Visual Studio中名为
app.config
)包含
connectionString
元素中名为“consting”的连接字符串的定义

例如:



请注意,您的代码中还有一些其他问题,例如,至少在
SqlCommand
对象上的即将发生的、丢失的
Dispose
调用(或
使用
块),等等。

Hi Mildred,此错误来自何处?是DB_访问类还是DB_连接类?你是怎样试图找出问题的?它认为这意味着你正在做某事的某个对象是空的。所以请告诉我哪个对象是空的问题是我不能理解它的确切含义我是一个新手,我正在尝试一种新的东西有没有内部异常显示更多细节?如果您将代码从静态构造函数移到方法中(仅用于测试),那么错误是否仍然会发生?您是否确保app.config或machine.config文件中存在名为“ConString”的连接字符串?您好,Mildred,此错误来自何处?是DB_访问类还是DB_连接类?你是怎样试图找出问题的?它认为这意味着你正在做某事的某个对象是空的。所以请告诉我哪个对象是空的问题是我不能理解它的确切含义我是一个新手,我正在尝试一种新的东西有没有内部异常显示更多细节?如果您将代码从静态构造函数移到方法中(仅用于测试),那么错误是否仍然会发生?您是否确保app.config或machine.config文件中存在名为“ConString”的连接字符串?
public static string constr = 
    ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;