问题描述
- 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;?????}?} }
解决方案
难道这代码是醉汉写的?
string sql = "SELECT age FROM [number] WHERE num="+a+"";
你能告诉我最后加上""是干嘛?
age = Convert.ToInt32(age );
age本身就是int,再转换成int又是干嘛
先搞清楚写程序是怎么一回事吧。