从SQL数据库检索数据到C#.Net [英] retriving data from SQL database to C#.Net

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

问题描述

大家好,
我正在使用C#.net,我必须从SQL数据库中检索序列号,例如A23,但我只需要下面的23个代码,这里的GenerateKey()是保存值的函数

Hi All,
I am using C# .net where I have to retrieve serial number from SQL database which is like A23 but I need only 23 below I am give code, here GenerateKey() is the function which hold the value

public string GenerateKey()
   {
       int data = 0;
       string id = "";
       string str = "select billno from tblEditBill"; //table name tblEditBill
       OdbcCommand cmd;
       cmd = new OdbcCommand(str, conn);
       OdbcDataReader dr;
       dr = cmd.ExecuteReader();
       while (dr.Read() != false)
       {
           data = Convert.ToInt32(dr.GetString(0));

       }
       try
       {

           int newid = Convert.ToInt32(data);
           //Int32.Parse(data);
           id = Convert.ToString(newid + 1);

       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message + ex.Source);
       }

       return id.ToString();
   }


如果可以的话,请帮忙.谢谢大家.

谢谢&问候
英德拉吉特


更新
您好朋友,您也可以像这样从字符串中手动删除您的字符,然后使用剩余的字符将其转换为整数以使用它们


If it is possible please help.Thanks to All.

Thanks & Regards
Indrajit


Update
Hello Friend You Can also manually remove your character from your string like this and use the remaining character to convert it into integer to use them

protected string GetInteger(string s)
{
      string str = s;
      string newStr = "";// string newStr = string.Empty;
      for (int i = 0; i < str.Length; i++)
      {
         // here i will check for the ascii value to match with 0123456789 then it will add to new string
          if (Convert.ToInt32(str[i]) > 47 && Convert.ToInt32(str[i]) < 58)
          {
             newStr += str[i];
          }
      }
     // now new gentrated string will be returned
     return newStr;
}

推荐答案

使用正则表达式仅获取数字
Use Regex to get only numbers
data = Convert.ToInt32(Regex.Match(dr.GetString(0), "\d+").Value);


不清楚要实现的目标,但是代码显示了很多问题;该代码在形式上是正确的,但是编码风格显示出许多误解:


  • string id = "";必须为:string id = string.Empty;.
  • string str = "select...";;
  • OdbcDataReader dr; dr = cmd.ExecuteReader();是正确的,但看起来很奇怪;
    为什么不使用OdbcDataReader dr = cmd.ExecuteReader();?
  • while (dr.Read() != false)表示误解布尔类型和条件;
    应为while (dr.Read())...
Not clear what you want to achieve but the code shows numerous problems; the code is formally correct, but coding style show a number of misunderstandings:


  • string id = ""; must be: string id = string.Empty;.
  • string str = "select...";; use of immediate string constant hard-coded in the method.
  • OdbcDataReader dr; dr = cmd.ExecuteReader(); is correct but looks strange;
    why not OdbcDataReader dr = cmd.ExecuteReader();?
  • while (dr.Read() != false) shows misunderstanding of Boolean types and conditions;
    should be while (dr.Read())...


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

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