有沒有辦法使用Xamarin和ASP.NET C#連接android到SQL服務器? 我已經看到了連接到SQLite而不是SQL服務器的方式。 如果這不可能,那麼是否有一種方法可以將android應用程序中的數據從SQLite同步到SQL服務器?連接Android應用程序到SQL服務器
任何幫助將不勝感激。
有沒有辦法使用Xamarin和ASP.NET C#連接android到SQL服務器? 我已經看到了連接到SQLite而不是SQL服務器的方式。 如果這不可能,那麼是否有一種方法可以將android應用程序中的數據從SQLite同步到SQL服務器?連接Android應用程序到SQL服務器
任何幫助將不勝感激。
可以使用的SqlConnection: https://developer.xamarin.com/api/type/System.Data.SqlClient.SqlConnection/
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Do work here; connection closed on following line.
}
編輯: 這是我在圖書館的一個發現,我修改了它一點點的方法。首先,您需要創建一個連接字符串,以便Xamarin知道要連接的位置。之後,我們使用SqlCommand
創建一個SQL命令,然後執行它。
public void Execute()
{
SqlConnectionStringBuilder dbConString = new SqlConnectionStringBuilder();
dbConString.UserID = "My Username";
dbConString.Password = "My Password";
dbConString.DataSource = "My Server Address";
using (SqlConnection con = new SqlConnection(returnConnectionString().ConnectionString))
{
con.Open();
for (int i = 0; i < commands.Count; i++)
{
SqlCommand cmd = new SqlCommand("UPDATE MyTable SET Name = 'New Name' WHERE ID = 1");
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
}
的ExecuteNonQuery()返回了多少行的影響,因而它的usuall當用於更新 - 和INSERT語句(即不是SELECT語句)。更多信息: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx }
如果你要使用SELECT語句的代碼返回結果爲一個DataSet:
public DataSet Execute(Statement stat)
{
DataSet ds = new DataSet();
SqlConnectionStringBuilder dbConString = new SqlConnectionStringBuilder();
dbConString.UserID = "My Username";
dbConString.Password = "My Password";
dbConString.DataSource = "My Server Address";
using (SqlConnection con = new SqlConnection(dbConString.ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", con);
var adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
}
return ds;
}
感謝您的鏈接,但如果有一個例子是真的很有幫助。 –
查看編輯答案。我希望它能幫助你! – Jefecito
謝謝你真的很有幫助: - ) –