nyfer
nyfer

Reputation: 95

web service to insert values into sql database

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

Upvotes: 0

Views: 15035

Answers (3)

nyfer
nyfer

Reputation: 95

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);
}

Upvotes: 1

Jim
Jim

Reputation: 2146

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.

Upvotes: 0

k to the z
k to the z

Reputation: 3185

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

Upvotes: 1

Related Questions