2011-04-14 37 views

回答

1

有一本很好的書叫做實用數據庫編程,用visual b#.NET編寫。

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

你想要的是一個Restful網絡服務。該鏈接是您的基本howto。

+0

確定我有asp.net應用程序,現在我想將值插入SQL數據庫。怎麼去吧 – nyfer 2011-04-14 16:05:36

+0

你在談論不同的事情。你可以有一個asp.net應用程序,就像一個表單,它接受表單值並提交給數據庫。或者你可以有一個你的asp.net應用程序調用的Web服務。我給出的鏈接顯示瞭如何執行Web服務選項。 – 2011-04-14 16:11:19

+0

Sory abt那,但是很困惑。 M客戶端即Windows Mobile將數據發送到Web服務。現在從webservice我想發送到SQL數據庫,我們已經在asp.net – nyfer 2011-04-14 16:18:04

0

您可能想要研究Linq To SQL它可以很好地將存儲過程封裝到c#方法中,以便您可以訪問數據。您也可以使用它來讀取和更新表格,但我個人更喜歡存儲過程。

+0

這是我的web方法[WebMethod] public string LocationInfo(String lat){ return lat; }現在我想要將lat拉入數據庫。該回報僅用於測試目的。 – nyfer 2011-04-14 16:10:59