如何将数据库中的记录插入标签? [英] How to insert records from database to a label?

查看:131
本文介绍了如何将数据库中的记录插入标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我在如何将数据库中的数据插入到指定的特定标签时遇到问题。首先,它会搜索基于记录的记录从我的两个文本框输入的信息。如果它匹配任何,它将显示将插入分配给它们的标签的信息,但我收到一条错误消息,索引超出了数组的范围。但我知道我正在访问数据库中的正确索引。谁能帮我吗?我今天也需要提出这个:(我真的需要一只手。



这是我的代码:

Hi Everyone!

I am having a problem on how to insert the data from the database to their specific labels assigned.First, it will search the records based from the inputted information from my two text boxes. If it matches any, it will display the information that will insert to the labels assigned to them but I got an error message,"Index was outside the bounds of the array". But I know to myself that I am accessing the right indexes in the database. Can anyone help me out? I need to present this also today :( and I really need a hand.

Here is my code:

connection_repgen();
        amicassaCon_repgen.Open();
        SqlCommand searchquery = new SqlCommand("SELECT ContractBuyerCode,ContractBuyerName,ContractSBU,ContractProjectName,ContractPrjUnitDesc,ContractModel,ContractStatus FROM MC.tblContracts WHERE ContractCompanyCode ='" + company_code.Text + "' AND ContractNo = '" + contract_no.Text + "'", amicassaCon_repgen);
        SqlDataReader dr = searchquery.ExecuteReader();
        while (dr.Read())
        {
            
            buyer_code.Text = dr[7].ToString();
            company_name.Text = dr[0].ToString();
            status.Text = dr[3].ToString();
            buyer_name.Text = dr[10].ToString();
            project_name.Text = dr[11].ToString();
            unit_desc.Text = dr[12].ToString();
            model.Text = dr[18].ToString();
           
           
        }
        dr.Close();
        amicassaCon_repgen.Close();

推荐答案

索引是基于select语句中列的位置,你有
index is based on position of column in your select statement, you have
ContractBuyerCode,ContractBuyerName,ContractSBU,ContractProjectName,ContractPrjUnitDesc,ContractModel,ContractStatus 



列。所以ContractBuyerCode有0索引,ContractBuyerName有索引1,所以...

相应地改变代码


columns in your select sql. so ContractBuyerCode has 0 index, ContractBuyerName has index 1, so on...
change the code accordingly


并添加到索引起点的答案:为了避免SQL注入,数据转换问题等,您应该在查询中使用 SqlParameter ,而不是直接将文本框中的值连接到SQL语句。



所以你的代码看起来像

And to add to the answer concerning indexing start point: To be safe from SQL injections, data conversion problems etc, you should use SqlParameter in your queries instead of directly concatenating values from the text boxes to the SQL statement.

So you code could look something like
...
SqlCommand searchquery = new SqlCommand(
"SELECT tc.ContractBuyerCode,                       
        tc.ContractBuyerName, 
        tc.ContractSBU, 
        tc.ContractProjectName, 
        tc.ContractPrjUnitDesc, 
        tc.ContractModel, 
        tc.ContractStatus 
FROM MC.tblContracts tc
WHERE tc.ContractCompanyCode = @ContractCompanyCode
AND   tc.ContractNo          = @ContractNo", amicassaCon_repgen);
   searchquery.Parameters.Add( new SqlParameter() {
      ParameterName = "@ContractCompanyCode",
      DbType = SqlDbType.VarChar,
      Size = 100,
      Value = company_code.Text }; // Remember to validate the data first?
   searchquery.Parameters.Add( new SqlParameter() {
      ParameterName = "@ContractNo",
      DbType = SqlDbType.Int,
      Value = contract_no.Text };
   SqlDataReader dr = searchquery.ExecuteReader();
...





欲了解更多信息,请参阅:

- SQL注入 [ ^ ]

- SqlParameter类 [ ^ ]


查看此



如何操作-i-set-my-labels-text-with-a-value-from-the-database [ ^ ]



希望它会有所帮助......
Check this

how-do-i-set-my-labels-text-with-a-value-from-the-database[^]

Hope it will help...


这篇关于如何将数据库中的记录插入标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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