編輯 2:通過使用 SqlConnection、SqlCommand 和 SqlDataReader 解決
免責宣告:我完全是使用 C# 代碼在 SQL 資料庫中搜索的初學者,所以我對這個問題幾乎一無所知。
我的任務是制作一個桌面應用程式,用于根據產品 ID 在資料庫中搜索產品名稱。我唯一成功做的就是通過 Visual Studio 將 SQL 資料庫連接到應用程式。在這張圖片上,您可以看到我能給出的最清晰的描述。
我完全不知道該嘗試什么。我見過一些名為 SqlConnection 的類,但同樣,我不完全確定它是否能以某種方式幫助我。
我確信這是這個網站上發布過的最基本的問題之一,但我完全卡住了,不知道下一步該怎么做。
提前感謝您的任何幫助。
編輯:是的,我忘了提及一件非常重要的事情 - 我將它作為 UWP 應用程式制作。對不起。
uj5u.com熱心網友回復:
對于您的方案,請參閱在 UWP應用檔案中使用 SQLite 資料庫。它有使用 Microsoft.Data.SQLite 使用特定 table-command 加載資料庫的詳細步驟。
根據產品 ID 在資料庫中搜索產品名稱。
SELECT * FROM [tb-name] WHERE [id]=@id"
selectCommand.Parameters.AddWithValue("@id", "id-value");
uj5u.com熱心網友回復:
首先,您需要為您的資料庫設定一些連接,將此行添加到您的 web.config 檔案中
<connectionStrings>
<add name="myDbConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后如果你設計了 UI Part,那么在你的 UI 上點擊按鈕后呼叫這個函式,
protected void Button1_Click(object sender, EventArgs e)
{
//data soure control that works with sql database
SqlDataSource sds = new SqlDataSource();
//get connection string from application's web.config file
sds.ConnectionString = ConfigurationManager.ConnectionStrings["myDbConnectionString1"].ToString();
//create parameters with specified name and values
sds.SelectParameters.Add("name", TypeCode.String, this.TextBox1.Text);
//set the sql string to retrive data from the database
sds.SelectCommand = "SELECT * FROM [myTb] WHERE [name]=@name";
//retrive data
DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
if (dv.Count == 0)
{
this.Label1.Text = "No Data Found";
return;
}
else
{
GridView1.DataSource = sds;
GridView1.DataBind();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/380092.html
