拆分字符串并在数据库中进行比较,然后将其返回. [英] Split the string and compare it in database return it.

查看:69
本文介绍了拆分字符串并在数据库中进行比较,然后将其返回.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用,(逗号)存储字符串,现在我想从comapre字符串中以其他价格取回数据.

我存储这样的字符串a,b,c,d,.字符串长度不同.有时是a,b,c,或者有时是a,b,c,d,

现在我想像
一样检索它

I store string using , (comma) now i want to retrieve data back from the comapre string in another with the price.

I store string like this a,b,c,d,. the string is in different length. some times it a,b,c, only or sometimes a,b,c,d,

Now i want to retrieve it like

name  price
a       10
b       20
c       30
total   60



我正在为此使用MS Access数据库和asp.net c#,请为此提供帮助

您好,我在这里修改了一些代码,但它给了我错误.



I am using MS access database and asp.net c# for this please help out for this

Hello here i right some code but its giving me error.

protected void Button1_Click(object sender, EventArgs e)
{
    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
                              @"Data source= C:\Ruchita\web\" + 
                              @"WebSite5\27\afcergonomic.mdb";
    OleDbConnection conn = new OleDbConnection(connectionString);
    string          s2   = TextBox1.Text.ToString();
    string          sql  = "select * from record where [recordid]=" + s2 + "";
    OleDbCommand    cmd  = new OleDbCommand(sql, conn);
    conn.Open();
    OleDbDataReader reader;
    //if (reader.Read())
    //{
    //    reader = cmd.ExecuteReader();
    //    string sred = reader.GetString(2).ToString();
    //    Response.Write(sred);
    //}
    reader = cmd.ExecuteReader();
    string sred="";
    while (reader.Read())
    {
        sred = reader.GetString(1).ToString();
        Response.Write(sred);
        //Response.Write(reader.GetString(1).ToString() + " ,");
        //Response.Write("");
    }
    reader.Close();
    int last = 0;
    for (int i = 0; i < sred.Length; i++)
    {
        if (sred[i] == ',')
        {
            OleDbCommand cmd1 = new OleDbCommand("Select productprice FROM polecart_parts where productname like" + 
                                "'%" + sred.Substring(last, i - last) + "%'", conn);
            string scmd1 = cmd1.ExecuteScalar().ToString() + "";
            Response.Write(scmd1);
            //Label1.Text += scmd1;
            last = i;
        }
        if (i == sred.Length - 1 && Convert.ToInt32(sred.Length[sred.Length - 1]) != ',')
        {
            Label1.Text += new OleDbCommand("Select productprice FROM polecart_parts where productname  like" + "'%" + sred.Substring(last, i - last) + "%'", conn).ExecuteScalar().toString();
        }
    }
    conn.Close();
}





Errors:CS0021: Cannot apply indexing with [] to an expression of type ''int''<br />
<br />
last = i;<br />
Line 62:         }<br />
Line 63:         if (i == sred.Length - 1 && Convert.ToInt32(sred.Length[sred.Length - 1]) != '','')<br />
Line 64:         {<br />
Line 65:             Label1.Text += new OleDbCommand("Select productprice FROM polecart_parts where productname  like" + "''%" + sred.Substring(last, i - last) + "%''", conn).ExecuteScalar().toString();


如果删除此代码,则它的NullRefrenceExepetion not handle


if remove this code then its : NullRefrenceExepetion not handle

推荐答案

我敢打赌sred仍然是空/空字符串.在此行之前:

I bet sred is still a null/emptry string. Before this line:

for (int i = 0; i < sred.Length; i++)



插入以下内容:



insert the following:

if (!string.IsNullOrEmpty(sred))
{


并在结束for循环的花括号后面放一个封闭的花括号.

您还应该认真考虑在整个代码块中使用try/catch/finally.


and put a closing brace around after the brace that ends the for loop.

You should also seriously consider using try/catch/finally around the entire code block.


您认真吗?

Are you serious?

if (i == sred.Length - 1 && Convert.ToInt32(sred.Length[sred.Length - 1]) != ',')



将由
修复



Will be fixed by

if (i == sred.Length - 1 && Convert.ToInt32(sred[sred.Length - 1]) != ',')



但是,您识别自己的代码的技能超出了我的理解范围.
您的错误消息告诉您确切的操作方法...
刚开始编程?不要着急,练习编写更简单的代码,直到您感到舒服为止.


该代码已经够糟糕的了,因此我不能保证(并且我不想)在此修复程序之后会发出提示,我只想指出您要问的问题. (请参阅约翰的回答).进一步的工作由您决定.

—SA



However, your skill in recognition of your own code is beyond my understanding.
Your error message told you exactly what to do...
Just started programming? Don''t rush, exercise writing simpler code until you''re comfortable.


The code is bad enough, so I cannot guarantee (and I don''t want to) it will word after this fix, I wanted only to point out the problem you''re asking about. (Please see John''s Answer). Further work is up to you.

—SA


这篇关于拆分字符串并在数据库中进行比较,然后将其返回.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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