如何从表中选择一个值并将其保存到变量中? [英] How can I select a value from table and save it into a variable?

查看:87
本文介绍了如何从表中选择一个值并将其保存到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要你的帮助。我有一个代码从Request表中获取最大值并将返回的值保存到变量中。然后使用带有insert语句的变量保存到Vacation表中,以便在我想显示请求时使用它。这是我的代码



Hello guys,

please I need your help. I have a code which get max value from Request table and save the returned value into a variable. then use this variable with insert statement to save into Vacation table to use it when I want display the requests. Here is my code

<pre lang="c#">

使用系统;

使用系统.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;



public partial class Vavation:System.Web.UI.Page

{

Dbclass db = new Dbclass();

string s =;

string d =;

int R;

string firstVariable = string.Empty;

protected void Page_Load(object sender,EventArgs e)

{

try

{

if(!IsPostBack)

{

tbid.Text = Session [uid]。 String();

lblDept.Text = Session [Did]。ToString();

lblid.Text = tbid.Text;

string s =Select * From Employee,其中National_ID ='+ lblid.Text +';

DataTable dt = db.getTable(s);

if (dt!= null&& dt.Rows.Count> 0)

{

tbid.Text = dt.Rows [0] [National_ID]。ToString();

tbname.Text = dt.Rows [0] [FirstName]。ToString()+''+ dt.Rows [0] [LastName]。ToString();

lblDept.Text = dt。行[0] [Dept_ID]。ToString();

}

string d =SELECT Balance FROM Vacation WHERE(National_ID ='+ lblid.Text + ')AND(StartDate =(SELECT MAX(StartDate)FROM Vacation WHERE(National_ID ='+ lblid.Text +')))AND(Balance =(SELECT MIN(Balance)FROM Vacation WHERE(National_ID ='+ lblid.Text +')));

DataTable t = db.getTable(d);

if(t!= null&& t.Rows .Count> 0)

{

lbldays.Text = t.Rows [0] [Balance]。ToString();

}

}



}

catch(Exception ex)

{

lberr.Text = ex.Message;

}

} < br $>


protected void btnadd_Click(object sender,EventArgs e)

{

AddVaction();

GetMax();

int Remaine = Convert.ToInt32(lbldays.Text);

DateTime start = DateTime.Parse(tbstartvaca.Text).Date;

if(Remaine< = 30&& Remaine> = 0&& start> = DateTime.Now)

{

TimeSpan重新输入;

DateTime end = DateTime.Parse(tbendvaca.Text).Date;

TimeSpan vacation = TimeSpan.Parse(lbldays.Text);

int Balance =(int)vacation.TotalDays;

TimeSpan total;

如果(开始>结束)

{

lberr.Text =请再次检查开始日期;

返回;

}



remaindate = end - start;

int days =(int)remaindate .TotalDays;

lberr.Text =你已离开+ remaindate.TotalDays +天。;

总计=假期 - 重新签名;

int T =(int)total.TotalDays;

lbldays.Text =你已离开+ total.TotalDays +天。;



s =Ins ert into Vacation(Balance,National_ID,NoOfDays,StartDate,EndDate,Dept_ID,Req_date,Req_ID)值('+ T +','+ lblid.Text +','+ days +',' + tbstartvaca.Text +','+ tbendvaca.Text +','+ lblDept.Text +','+ DateTime.Now +','+ R +');

db.Run(s);



}

else

{

lberr.Text =你没有度假或休假的开始时间少于一天的日期;

}



}



private void GetMax()

{

d =select MAX( Req_ID)来自Request;

lblReq.Text = d;

DataTable dt = db.getTable(d);

if(dt! = null&& dt.Rows.Count> 0)

{

R = Convert.ToInt32(dt.Rows [0] [Req_ID]。ToString());



}



}



private void AddVaction()

{

s =插入请求(National_ID,Type_Req,Dept_ID,Date)值('+ lblid.Text +','Vacation','+ lblDept.Text +','+ DateTime.Now +');

db.Run(s);



}

}



当我运行代码时我得到Max Req_ID为0,尽管没有错误的代码和Max Req_ID应该是最新的价值。



请有人有想法解决问题帮助我。我不是问题所在。它是在数据库结构中还是在代码中。



我尝试过:



我尝试了很多代码但是我没有得到任何价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Vavation : System.Web.UI.Page
{
Dbclass db = new Dbclass();
string s = "";
string d = "";
int R;
string firstVariable = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if(!IsPostBack)
{
tbid.Text = Session["uid"].ToString();
lblDept.Text = Session["Did"].ToString();
lblid.Text = tbid.Text;
string s = "Select * From Employee where National_ID='" + lblid.Text + "'";
DataTable dt = db.getTable(s);
if (dt != null && dt.Rows.Count > 0)
{
tbid.Text = dt.Rows[0]["National_ID"].ToString();
tbname.Text = dt.Rows[0]["FirstName"].ToString() + ' ' + dt.Rows[0]["LastName"].ToString();
lblDept.Text = dt.Rows[0]["Dept_ID"].ToString();
}
string d = "SELECT Balance FROM Vacation WHERE (National_ID = '" + lblid.Text + "') AND (StartDate =(SELECT MAX(StartDate) FROM Vacation WHERE (National_ID ='" + lblid.Text + "'))) AND (Balance = (SELECT MIN(Balance) FROM Vacation WHERE (National_ID ='" + lblid.Text + "')))";
DataTable t = db.getTable(d);
if (t != null && t.Rows.Count > 0)
{
lbldays.Text = t.Rows[0]["Balance"].ToString();
}
}

}
catch(Exception ex)
{
lberr.Text = ex.Message;
}
}

protected void btnadd_Click(object sender, EventArgs e)
{
AddVaction();
GetMax();
int Remaine = Convert.ToInt32(lbldays.Text);
DateTime start = DateTime.Parse(tbstartvaca.Text).Date;
if (Remaine <= 30 && Remaine >= 0 && start >= DateTime.Now)
{
TimeSpan remaindate;
DateTime end = DateTime.Parse(tbendvaca.Text).Date;
TimeSpan vacation = TimeSpan.Parse(lbldays.Text);
int Balance = (int)vacation.TotalDays;
TimeSpan total;
if (start > end)
{
lberr.Text = "Please check again on Starting date";
return;
}

remaindate = end - start;
int days = (int)remaindate.TotalDays;
lberr.Text = "you have left with " + remaindate.TotalDays + "days.";
total = vacation - remaindate;
int T = (int)total.TotalDays;
lbldays.Text = "you have left with " + total.TotalDays + "days.";

s = "Insert into Vacation (Balance, National_ID, NoOfDays, StartDate, EndDate, Dept_ID, Req_date, Req_ID) values ('" + T + "', '" + lblid.Text + "', '" + days + "', '" + tbstartvaca.Text + "', '" + tbendvaca.Text + "', '" + lblDept.Text + "', '" + DateTime.Now + "', '" + R + "')";
db.Run(s);

}
else
{
lberr.Text = "You Don't a Balance of Vacation OR Start of Vacation is less than date of day ";
}

}

private void GetMax()
{
d = "select MAX(Req_ID) from Request";
lblReq.Text = d;
DataTable dt = db.getTable(d);
if (dt != null && dt.Rows.Count > 0)
{
R = Convert.ToInt32(dt.Rows[0]["Req_ID"].ToString());

}

}

private void AddVaction()
{
s = "Insert into Request (National_ID, Type_Req, Dept_ID,Date) values ('" + lblid.Text + "', 'Vacation','" + lblDept.Text + "', '" + DateTime.Now + "')";
db.Run(s);

}
}

when I run the code I get Max Req_ID "zero" although the code running with out errors and the Max Req_ID should be the latest value.

please anyone has idea to solve the problem help me. I do not where the problem is. is it in database structure or in the code.

What I have tried:

I tried many code but I could not get any value.

推荐答案

暂时不要担心 - 你有更严重的问题!

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



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

Don't worry about that for a moment - you have much more serious problems!
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?

And in a website? Where anyone in the world can get access? You have to fix this immediately throughout the whole of your software - or your DB (and the site it serves) will not last long...


查看该代码,我很漂亮确定问题出在代码中。



我没有为您提供解决方案,但会提供一些实用的建议,可以帮助您找到解决方案:

•您的代码容易受到SQL注入攻击 - 参数化你的输入&不要依赖它们在你的SQL语句中安全使用。

•切换到使用ORM。

•你的方法太忙 - 每个方法只做一件事。拥有更简单的方法比仅有一些非常复杂的方法更好。

•你的方法GetMax()是无效的 - 而是让它返回值。
Looking at that code, I'm pretty sure the problem is in the code.

I don't have a solution for you but will offer some practical advice which may help you find the solution:
• Your code is vulnerable to SQL injection attack - parameterize your inputs & don't rely on them to be safe to use in your SQL statement.
• Switch to using an ORM.
• Your methods are too "busy" - have each method do one thing only. It's better to have more simplistic methods than only a few really complex ones.
• Your method GetMax() is void - rather let it return the value instead.


这篇关于如何从表中选择一个值并将其保存到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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