web service to insert values into sql database

十年热恋 提交于 2019-12-25 00:07:06

问题


I am sending some data from windows mobile to webservice. Now i want to write a method in webservice to insert those values into sql database . How to proceed. please help


回答1:


What you want is a Restful web service. That link is your basic howto.




回答2:


there is good book called practical database programming with visual c#.NET by ying bai.

//base class
public class SQLInsertBase
{
  public bool SQLInsertOK;
  public string SQLInsertError;
  public string lat;
  public string lon;
  public string id;
  public SQLInsertBase()
{
    //
    // TODO: Add constructor logic here
    //
   }
}



  [WebMethod]
public SQLInsertBase GetSQLInsertCourse(string id,string lat,string lon)
{
    string cmdString = "INSERT into Location values(@id,@latitude,@longitude)";
    SqlConnection sqlConnection = new SqlConnection();
    SQLInsertBase GetSQLResult = new SQLInsertBase();
    SqlDataReader sqlReader;
    GetSQLResult.SQLInsertOK = true;
    sqlConnection = SQLConn();
    if (sqlConnection == null)
    {
        GetSQLResult.SQLInsertError = "Database connection is failed";
        ReportError1(GetSQLResult);
        return null;
    }
    SqlCommand sqlCommand = new SqlCommand(cmdString, sqlConnection);
    sqlCommand.CommandType = CommandType.Text;
    sqlCommand.Parameters.Add("@id", SqlDbType.Text).Value = id;
    sqlCommand.Parameters.Add("@latitude", SqlDbType.Text).Value = lat;
    sqlCommand.Parameters.Add("@longitude", SqlDbType.Text).Value = lon;
    int result = sqlCommand.ExecuteNonQuery();
  //  if (sqlReader.HasRows == true)
   //     FillCourseDetail(ref GetSQLResult, sqlReader);
    if (result == 0)
    {
        GetSQLResult.SQLInsertError = "cannot update ";
        ReportError1(GetSQLResult);
    }
   /* else
    {
        GetSQLResult.SQLInsertError = "No matched  found";
        ReportError1(GetSQLResult);
    }*/
  //  sqlReader.Close();
    sqlConnection.Close();
    sqlCommand.Dispose();
    return GetSQLResult;
}


 protected SqlConnection SQLConn()
  {
    string cmdString =     ConfigurationManager.ConnectionStrings["sql_conn"].ConnectionString;
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = cmdString;
    conn.Open();
    if (conn.State != System.Data.ConnectionState.Open)
    {
        MessageBox.Show("Database Open  failed");
        conn = null;
    }
    return conn;
}


protected void ReportError1(SQLInsertBase ErrSource)
{
    ErrSource.SQLInsertOK = false;
    MessageBox.Show(ErrSource.SQLInsertError);
}



回答3:


You may want to look into Linq To SQL which does a nice job of encapsulating your stored procedures into c# methods so you can access your data. You can also use it to read and update tables, but I prefer stored procedures personally.



来源:https://stackoverflow.com/questions/5665884/web-service-to-insert-values-into-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!