so_chain 2015-03-22 06:02 采纳率: 0%
浏览 2032

websevice提示从索引 0 处开始,初始化字符串的格式不符合规范。ADD功能不能用 第二个可以

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

namespace WebService2
{
///
/// Service1 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public int   GetData(int   a)
    {
        string strcon = " Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=kong9393";

        SqlConnection mycon;
        mycon = new SqlConnection(strcon);
        string sql = "SELECT age FROM [number] WHERE num="+a+"";

      SqlCommand mysc = new SqlCommand(sql, mycon);
        mycon.Open();
        SqlDataReader rd = mysc.ExecuteReader();

        int age=0;

        while (rd.Read())
        {
            age  = (int )rd["age"];
        }
        age = Convert.ToInt32(age );
        return age ;
    }
   // 表为User,字段有 编号: int id,用户名:string UserName,密码:string UserPwd    /// <summary>     /// 添加           /// </summary> 

    /// 用户名     /// 密码     ///    
[WebMethod] 
     public int Add(int  num,int  age)   

        SqlConnection conn = new SqlConnection("连接字符串");  
conn.Open(); 
        string sql = "intsert into number (num,age) values(@num,@age)"; 
        SqlCommand cmd = new SqlCommand(sql, conn); 
        SqlParameter parn = new SqlParameter("@num", num);    
cmd.Parameters.Add(parn); 
        SqlParameter parp = new SqlParameter("@age", age);    
cmd.Parameters.Add(parp); 
        int result = cmd.ExecuteNonQuery();     
conn.Close();     
cmd.Dispose();   
return result;     } 

    }

    }
  • 写回答

1条回答 默认 最新

  • threenewbee 2015-03-22 10:12
    关注

    难道这代码是醉汉写的?
    string sql = "SELECT age FROM [number] WHERE num="+a+"";
    你能告诉我最后加上""是干嘛?

    age = Convert.ToInt32(age );
    age本身就是int,再转换成int又是干嘛

    先搞清楚写程序是怎么一回事吧。

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料