2 happyleo12 happyleo12 于 2016.09.27 09:21 提问

asp.net中按钮点击发送短信功能,短信内容不能从数据库里提取到,这是怎么回事?

我网站采用的是三层架构,以下是在UI的cs页面上按钮点击事件
mysql语句已验证过,语句没问题
然后我在想,这个问题肯定是没有连接上数据库导致没查询,请问怎么加上连接数据库的代码呢?

 protected void sndsms_Click(object sender, EventArgs e)
        {
            string cont="";
            string str;
            string phones = "XXXXXXXXXX";
            string content = "";
            content = "系统:";
            content += cont;
            cont = @"SELECT a.NZLID as NozzleID,concat(Round(a.LASTAVG*100,2) ,'%') as `ErrorRate(%)`,
                                h.NZLERR-h.LASTERR as NozzleErr,
                                a.MACHINE,a.NZLMODULENO
                                FROM T_NZLMST4 a 
                                left join (select NZLID,NZLTOTALCNT,LASTCNT,NZLERR,LASTERR from T_NZLHIS4 b inner join 
                                (select max(id) as id from T_NZLHIS4 group by NZLID) c on b.id = c.id) h on a.NZLID=h.NZLID 
                                Order By case when h.NZLTOTALCNT=h.LASTCNT then 1 else 0 end,LASTAVG desc;";
            str = "短信服务商提供的接口“
            str += phones;
            str += "&msg=";
            str += content;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(str);
            req.Method = "GET";

            HttpWebResponse response = req.GetResponse() as HttpWebResponse;
            Stream respStream = response.GetResponseStream();
            StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
            string result = respStreamReader.ReadToEnd();
            //return strBuff; 
            if (result != "发送成功")
                Console.WriteLine("发送失败");
            else
                Console.WriteLine("发送成功");  
        }

1个回答

caozhy
caozhy   Ds   Rxr 2016.09.27 11:58
已采纳

没看到你执行sql的逻辑。
你应该写 SqlCommand cmd = new SqlCommand(连接);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
读取数据库返回的内容
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!