使用未分配的局部变量'dt' [英] use of unassigned local variable 'dt'

查看:124
本文介绍了使用未分配的局部变量'dt'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





每当我编译项目时,我都会收到错误使用未分配的局部变量'dt'。我的代码是:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Data.SqlClient;
使用 System.Windows.Forms;
使用 System.Data;

命名空间 DAL
{
public class dbHelper
{
SqlConnection SQLConn;
string connectionstring;
SqlDataAdapter da;
public void openconnection()
{
connectionstring = Server = GITTWO-PC\\SQL2008R2; Database = emp; User Id = accpac; Password = super1;;
try
{
SQLConn = new SqlConnection(connectionstring);
SQLConn.Open();
}
catch (Exception ex)
{
MessageBox.Show( 错误连接到数据库! - + ex.Message, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

public void closeconnection( )
{
SQLConn.Close();
}

public DataTable getdt( string query)
{
openconnection();
da = new SqlDataAdapter(query,SQLConn);
DataTable dt;
da.Fill(dt);
closeconnection();
return dt;
}

public Boolean executequery( String query)
{
openconnection();
SqlCommand com = new SqlCommand(query,SQLConn);
com.ExecuteNonQuery();
closeconnection();
return true ;
}
}
}

解决方案

而不是

< pre lang =c#> DataTable dt;



写...

 DataTable dt =  new  DataTable(); 





因为你还没有实例化DataTable对象,那就是为什么它显示错误。


未初始化局部变量。您必须手动初始化它们。

在此行的代码中将其更改为以下内容;

 DataTable dt =  new  DataTable(); 





祝你好运,

OI


Hi,

I am getting the error as "use of unassigned local variable 'dt'" whenever i am compiling my project. My Code is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace DAL
{
    public class dbHelper
    {
        SqlConnection SQLConn;
        string connectionstring;
        SqlDataAdapter da;
        public void openconnection()
        {
            connectionstring = "Server=GITTWO-PC\\SQL2008R2;Database=emp;User Id=accpac;Password=super1;";
            try
            {
                SQLConn = new SqlConnection(connectionstring);
                SQLConn.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show(" Error Connecting to database! - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void closeconnection()
        {
            SQLConn.Close();
        }
            
        public DataTable getdt(string query)
        {
            openconnection();
            da = new SqlDataAdapter(query, SQLConn);
            DataTable dt;
            da.Fill(dt);
            closeconnection();
            return dt;
        }
    
        public Boolean executequery(String query)
        {
            openconnection();
            SqlCommand com =new SqlCommand(query, SQLConn);
            com.ExecuteNonQuery();
            closeconnection();
            return true;
        }
    }
}

解决方案

Instead of

DataTable dt;


Write...

DataTable dt = new DataTable();



As you have not instantiated the DataTable object, that's why it is showing the error.


Local variables aren't initialized. You have to manually initialize them.
On your code in this line change it to the following;

DataTable dt = new DataTable();



Good luck,
OI


这篇关于使用未分配的局部变量'dt'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆