如何删除此错误(包括带有数据库连接的C#),其da.fill(dt)错误。 [英] How do I remove this error (includes C# with database connectivity) , its da.fill(dt) error.

查看:147
本文介绍了如何删除此错误(包括带有数据库连接的C#),其da.fill(dt)错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Data;

使用System.Data.SqlClient;



///< summary>

/// DataBaseClass的摘要描述

///

公共类DataBaseClass

{

SqlDataAdapter da;

SqlConnection con;

SqlCommand cmd = new SqlCommand();

DataSet ds = new DataSet();

DataTable dt = new DataTable();



public DataBaseClass()

{

//

// TODO:在这里添加构造函数逻辑

//

}



public void ConnectDataBaseToInsert(string Query)

{

con = new SqlConnection(@Data Source = RAHUL-SqlServer2008; Initial Catalog = RNetworkingWebApplication; uid = sa ; pwd = wintellect;);

cmd.CommandText = Query;

cmd.Connection = con;

da = new SqlDataAdapter(cmd);

con.Open();

cmd.ExecuteNonQuery();

con.Close();



}

public DataSet ConnectDataBaseReturnDS(string Query)

{

ds = new DataSet();

con = new SqlConnection(@Data Source = RAHUL-SqlServer2008; Initial Catalog = RNetworkingWebApplication; uid = sa ; pwd = wintellect;);

cmd.CommandText =查询;

cmd.Connection = con;

da = new SqlDataAdapter(cmd );

da.Fill(ds);

con.Open();

cmd.ExecuteNonQuery();

con.Close();

返回ds;

}

公共DataTable ConnectDataBaseReturnDT(字符串查询)

{

dt = new DataTable();

con = new SqlConnection(@Data Source = [(localdb)\ MSSQLLocalDB]; Initial Catalog = Database; uid = Utkarsh; pwd = password123; );

cmd.CommandText =查询;

cmd.Connection = con;

da = new SqlDataAdapter(cmd);

da.Fill(dt);

con.Open();

cmd.ExecuteNonQuery();

con.Close ();

返回dt;

}

显示: -



System.Data.dll中出现'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理



我拥有什么尝试过:



我尝试将数据库更改为新数据库但是徒劳无功..

解决方案

开始通过在调试器中运行它,当它失败时它将显示异常 - 然后你可以查看更详细的内容,特别是在任何内部异常,这可以更详细地解释问题是什么。另外仔细看看你传入的查询字符串 - 这很可能导致问题,但是你没有显示调用它的代码,所以我们无法直接帮助你。

但是......空格在SQL中很重要,因此有空格的文本需要用引号分隔,带空格的名称需要用方括号括起来。

但我们不能做任何测试或为您调试:我们无法访问您的其余代码或数据 - 您需要同时调试此类问题。



和听理查德所说的:如果你要连接字符串以形成你的SQL查询,那么这意味着你的用户可以做任何他们喜欢的事情,包括删除你的数据库。并通过您的帐户登录?这很危险!

就像骑手一样,不要对连接字符串进行硬编码:它们使得发布软件非常困难。请改用配置文件,以便在调试和发布之间不必更改代码。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DataBaseClass
///
public class DataBaseClass
{
SqlDataAdapter da;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

public DataBaseClass()
{
//
// TODO: Add constructor logic here
//
}

public void ConnectDataBaseToInsert(string Query)
{
con = new SqlConnection(@"Data Source=RAHUL-SqlServer2008; Initial Catalog=RNetworkingWebApplication; uid=sa; pwd=wintellect;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
public DataSet ConnectDataBaseReturnDS(string Query)
{
ds = new DataSet();
con = new SqlConnection(@"Data Source=RAHUL-SqlServer2008; Initial Catalog=RNetworkingWebApplication; uid=sa; pwd=wintellect;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public DataTable ConnectDataBaseReturnDT(string Query)
{
dt = new DataTable();
con = new SqlConnection(@"Data Source=[(localdb)\MSSQLLocalDB]; Initial Catalog=Database; uid=Utkarsh; pwd=password123;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return dt;
}
It shows :-

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

What I have tried:

I tried changing the databases to the new ones but in vain..

解决方案

Start by running this in the debugger, and when it fails it will show you the exception - you can then look in more detail at that, specifically at any inner exception which may explain in more detail what the problem is. Also look closely at the Query string you pass in - it's very likely that that is causing the problem, but you don't show the code which calls this, so we can't help with that directly.
But...spaces are important in SQL, so text that has spaces needs to be delimited with quotes, and names with spaces need bracketing with square brackets.
But we can't do any testing or debugging for you: we don't have access to the rest of your code, or to your data - and you need both to debug problems like this.

And listen to what Richard has said: if you are concatenating strings to form your SQL queries, then that means your users can do anything they like, up to an including deleting your database. And logging in via your sa account? That's just plain dangerous!
And just as a rider, don't hard code connection strings: they make it very difficult to release software. Use configuration files instead, so that the code doesn't have to be changed between debug and release.


这篇关于如何删除此错误(包括带有数据库连接的C#),其da.fill(dt)错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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