注册 登录
编程论坛 C# 论坛

初学C#,请教如何变量取值的问题

kent73 发布于 2015-07-27 10:04, 1394 次点击
之前通过如下代码能成功执行
string sql = "select count(*) as result from table where  FBarCode='" + barcode + "' and FCategory='" + lbie + "'";
int res = SQLSERVERUtil.GetRecordCount(sql);
if (res < 1)    //数据不存在
{
   labNotice.Text = "该数据不存在";
   temp_del = false;
}

那么我用下面的代码,怎么把ID赋值给变量
string sql = "select id from table where  FBarCode='" + barcode + "' and FCategory='" + lbie + "'and FUpdate=1";

谢谢了
5 回复
#2
Maick2015-07-27 11:57
书上没教吗?
#3
kent732015-07-27 11:59
回复 2楼 Maick
这是一段现有的程序,我需要在程序中间加入一段代码。
能帮我改改吗
#4
Maick2015-07-27 13:25
SQLSERVERUtil这个类里面应该有写了返回DATATABLE的方法的,你在用datatable 取值就是了
#5
kent732015-07-27 14:13
回复 4楼 Maick
SQLSERVERUtil完整代码是这样的,能帮我结合了改改吗?
class SQLSERVERUtil
    {

        private static SoundPlayer simpleSound;
        public static string errorpath = "\\Flash Disk\\error.wav";
        public static string rightpath = "\\Flash Disk\\right.wav";

        private static SqlConnection conn = GetConnection();
        private static SqlCommand cmd = null;
        public static SqlConnection GetConnection()
        {
            string connectionString = "";
            try
            {
                SQLSERVERconfig serverConfig = FileUtil.GetConfiguration();
                if (serverConfig.server!=null)
                {
                    connectionString = "Data Source=" + MainForm.userform.option.ReadString("Info", "IP", "") ;
                }
                if (serverConfig.database!=null)
                {
                    connectionString = connectionString + ";initial catalog=" + MainForm.userform.option.ReadString("Info", "DataName", "");
                }
                if (serverConfig.uid != null)
                {
                    connectionString = connectionString + ";UID=" + MainForm.userform.option.ReadString("Info", "User", "");
                }
                if (serverConfig.pwd != null)
                {
                    connectionString = connectionString + ";PWD=" + MainForm.userform.option.ReadString("Info", "Password", "");
                }
               
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(connectionString);
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message);
            }

            return connection;
        }
#6
Maick2015-07-27 15:48
你去百度搜索DBhelp吧..我也是新手不会啊
1