如何从数据库中获取数据并放入变量中
问题描述:
string querycheck = "SELECT sponcorid FROM Registration_Master WHERE regCode='" + temp + "'";
//string parent = (string)com.ExecuteScalar();
com = new SqlCommand(querycheck, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter(com);
DataTable dtcheck = new DataTable();
da.Fill(dtcheck);
string parent = dtcheck.Rows[0]["sponcorid"].ToString();
我希望将此sponcorid数据库值放入此父变量中,当我使用此逻辑时,该怎么做才能为我提供空白值,请帮帮我
添加了代码块[/编辑]
I want this sponcorid database value should be put in this parent variable how can do this it gives me blank value when i use this logic, please help me
Code block added[/Edit]
答
首先,检查您的temp
变量-确保内容符合您的期望.
然后,检查数据库,并确保至少一行具有匹配的regCode
值-完全匹配,包括任何空格.
最后,检查匹配的行在sponcorid
列中是否有值.
可能是某种形式的数据问题-上面的一种会抓住它.
顺便说一句:我不知道您从哪里获得temp
值,但是要小心-连接字符串是构建SQL命令的危险方式.它使您更容易受到SQL注入攻击的攻击,该攻击可能会意外或故意破坏或破坏您的数据库.最好始终使用参数化查询.
First, check yourtemp
varaible - make sure the content is what you are expecting.
Then, check your database and make sure that at least one row has a matchingregCode
value - exactly matching, including any spaces.
Finally, check that the matching rows have a value in thesponcorid
column.
Chances are it''s a data problem of some sort - and one of the above will catch it.
BTW: I don''t know where you get yourtemp
value from, but be careful - concatenating strings is a dangerous way to build an SQL command. It leaves you wide open to an SQL Injection attack which can accidentally or deliberately damage or destroy your database. It is a good idea to use parametrized queries at all times instead.
您好
嗨
您确定,如果直接在数据库中执行此查询([temp中的值]),您将得到结果吗?因为我猜想,这可能是您的查询/数据问题.您可以使用"temp"中的数据执行查询并检查结果吗?如果得到结果,则在代码中使用相同的查询.这只是为了确认数据是否存在任何问题.
IE .例如:
字符串querycheck ="SELECT sponcorid FROM Registration_Master WHERE regCode =``001''
除了使用DataTable,还可以使用DataSet进行检查.
问候
多米尼克
Hi
Hi
Are you sure, if you execute this query [with value in temp] in database directly, you will get the result? Because i guess, it might be the issue with your query/data . Can you execute the query with data in ''temp'' and check the result. If you get the result , then you use the same query in your code. This is for just to confirm whether there is any issue with data.
I.E . For Ex:
string querycheck = "SELECT sponcorid FROM Registration_Master WHERE regCode = ''001''
And instead of using DataTable, can you check by using DataSet.
Regards
Dominic