无法从数据库中检索数据 [英] Cannot retrieve data from database

查看:158
本文介绍了无法从数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想从sql server检索数据,但我的代码无法运行。它没有显示任何错误。但它仍然无法显示数据。有人能帮我吗。谢谢



我尝试过:



使用系统;

使用System.Collections.Generic;

使用System.Configuration;

使用System.Data.SqlClient;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.Odbc;

使用System.Collections.Specialized;

使用System.Text;

使用System.Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Drawing;





公共部分类EditStatus:System.Web.UI.Page

{

string connectionString = @Data Source = P13L1TNN225\SQLEXPRESS; Initial Catalog = EMPLOYEE1; Integrated Security = True;;

SqlCommand cmd = new SqlCommand();

SqlConnection con = new SqlConnection();

SqlDataAda pter sda = new SqlDataAdapter();

DataSet ds = new DataSet();





protected void Page_Load(对象发件人,EventArgs e)

{







使用(SqlConnection con = new SqlConnection(connectionString))

{

string query =从Ticket中选择* TicketID ='+ TicketID.Text +'; <使用(SqlCommand cmd = new SqlCommand(query,con))

{

con.Open() ;



SqlDataReader dr = cmd.ExecuteReader();

if(dr.Read())

{

TicketID.Text = dr [TicketID]。ToString();

lblEmpID.Text = dr [Emp_id]。ToString();

lblEmpName.Text = dr [Emp_Name]。ToString();

lblEmpEm ail.Text = dr [Emp_Email]。ToString();

lblCnum.Text = dr [Emp_Cnum]。ToString();

lblDpt.Text = dr [Emp_Dpt]。ToString();

lblPlant.Text = dr [Emp_Plant]。ToString();

lblSeverity.Text = dr [Emp_Severity ] .ToString();

lblSubject.Text = dr [Emp_Subject]。ToString();

lblDscri.Text = dr [Emp_Dscri]。ToString ();

lblStatus.Text = dr [Emp_Status]。ToString();

lblDatetime.Text = dr [Datetime]。ToString(); < br $>


}

con.Close();

con.Dispose();



}



}

Hi guys, i want to retrieve data from sql server but my code are not functioning. It doesn't show any error. But it still cannot display the data. Can someone help me. Thanks

What I have tried:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
using System.Collections.Specialized;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;


public partial class EditStatus : System.Web.UI.Page
{
string connectionString = @"Data Source=P13L1TNN225\SQLEXPRESS; Initial Catalog = EMPLOYEE1; Integrated Security=True;";
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{



using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "Select * from Ticket where TicketID='" + TicketID.Text + "'";

using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TicketID.Text = dr["TicketID"].ToString();
lblEmpID.Text = dr["Emp_id"].ToString();
lblEmpName.Text = dr["Emp_Name"].ToString();
lblEmpEmail.Text = dr["Emp_Email"].ToString();
lblCnum.Text = dr["Emp_Cnum"].ToString();
lblDpt.Text = dr["Emp_Dpt"].ToString();
lblPlant.Text = dr["Emp_Plant"].ToString();
lblSeverity.Text = dr["Emp_Severity"].ToString();
lblSubject.Text = dr["Emp_Subject"].ToString();
lblDscri.Text = dr["Emp_Dscri"].ToString();
lblStatus.Text = dr["Emp_Status"].ToString();
lblDatetime.Text = dr["Datetime"].ToString();

}
con.Close();
con.Dispose();

}

}

推荐答案

两件事:

1)它不显示任何数据,因为没有行匹配。检查TicketID.Text中的确切内容,然后手动查看数据库中的确切字符串。要成功进行=比较,字符串必须完全匹配,包括任何空格。



2)不要那样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Two things:
1) It doesn't display any data because no rows match. Check what - exactly - you have in TicketID.Text and then manually look through your database for that exact string. For an "=" comparison to succeed, the strings must match exactly, including any whitespace.

2) Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期进行备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


你在页面加载事件上编写了代码,请调试并确保你在TicketID.Text中确实有一些数据。如果没有,请在将数据提供给TicketID之后,在其他事件上调用此代码,可能是在按钮点击事件上。



我还建议您理解ASP.Net页面生命周期(你会在CP上找到一些好文章)。



另一点,编写SQL查询不是一个好主意您编写的方式。



这称为连接SQL查询,它们引入了SQL注入的漏洞。

假设,用户在文本框中输入以下文本TicketID56; drop table XYZ;

现在当你在行中连接这个值时,SQL会收到两条指令,一条是获取数据的给出票证ID,另一个是删除表格XYZ。

这是一个严重的问题,你不希望用户能够从你的数据库中删除表格。



更好的方法是编写参数化SQL查询。阅读更多相关信息此处。
You wrote the code on the page load event, please debug and make sure that you actually have some data in "TicketID.Text". If not, call this code on some other event after you have given the data to TicketID, possibly on a Button click event.

I also suggest you get a good understanding of the ASP.Net page lifecycle (you will find some good articles on CP).

Another point, It is not a good idea to write SQL queries the way you have written.

This is called concatenated SQL queries and they introduce a vulnerability towards SQL injection.
Suppose, a user enters the following text in the textbox TicketID "56; drop table XYZ;"
Now when you concatenate this value in the line SQL will receive two instructions, one is to get the data for the given ticket id and the other is to delete the table XYZ.
That is a serious issue, you do not want users to be able to delete tables from your database.

A better approach is to write parametrized SQL queries. Read more about it here.

这篇关于无法从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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