正在读取使用参数查询数据库以在文本框中显示结果
本文关键字:文本 显示 结果 查询 读取 参数 数据库 | 更新日期: 2023-09-27 18:21:43
我目前正在构建一个数据库字体端,但我目前陷入了困境。我试图获取数据源下拉列表的List项,并将其用作SQL查询中的参数。然后,我希望这些结果中的每一列都显示在一个文本框中。这是一些代码!
按钮事件:
protected void ButtonAsset_Click(object sender, EventArgs e)
{
ExecuteSelect(DropDownListAsset.Selecteditem.Text);
}
获取连接字符串:
public string GetConnectionStringMyConn()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["my_conn"].ConnectionString;
}
选择方法:
private void ExecuteSelect(string Aname)
{
SqlConnection connection = new SqlConnection(GetConnectionStringMyConn());
using (var command = connection.CreateCommand())
{
command.CommandText = "Select tblAssets.AssetID, tblAssets.Domain, tsysOS.OSname, tblAssets.SP,"
+ "tblAssets.Memory, tblAssets.Processor, tblAssetCustom.Manufacturer, tblAssetCustom.Model)"
+ "FROM tblAssets"
+ "INNER JOIN tblAssetsCustom ON tblAssets.AssetID = tblAssetCustom.AssetID "
+ "INNER JOIN tsysOS ON tblAssets.OScode = tsysOS.OScode "
+ "WHERE tblAssets.AssetName = @AssetName";
connection.Open();
SqlParameter[] Aparam = new SqlParameter[1];
Aparam[0] = new SqlParameter("@AssetName", SqlDbType.NVarChar);
Aparam[0].Value = Aname;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
TextBoxAssetID.Text = reader["AssetID"].ToString();
TextBoxDomain.Text = reader["Domain"].ToString();
TextBoxOS.Text = reader["OSname"].ToString();
TextBoxSP.Text = reader["SP"].ToString();
TextBoxMemory.Text = reader["Memory"].ToString();
TextBoxProcessor.Text = reader["Processor"].ToString();
TextBoxManufacturer.Text = reader["Manufacturer"].ToString();
TextBoxModel.Text = reader["Model"].ToString();
}
connection.Close();
}
}
任何帮助都将不胜感激!我在使用(var reader=command.ExecuteReader())时不断出现语法错误,我不明白为什么。我一直在使用这个获取SQL数据并将其显示在文本框中?对于读取器
喜欢这个
SqlCommand command= new SqlCommand();
command.Connection = connection;
command.CommandText = "Select tblAssets.AssetID, tblAssets.Domain, tsysOS.OSname, tblAssets.SP,"
+ " tblAssets.Memory, tblAssets.Processor, tblAssetCustom.Manufacturer, tblAssetCustom.Model"
+ " FROM tblAssets"
+ " INNER JOIN tblAssetsCustom ON tblAssets.AssetID = tblAssetCustom.AssetID "
+ " INNER JOIN tsysOS ON tblAssets.OScode = tsysOS.OScode "
+ " WHERE tblAssets.AssetName = @AssetName";
connection.Open();
command.Parameters.Add("@AssetName",SqlDbType.NVarchar).Value = Aname;
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
TextBoxAssetID.Text = reader["AssetID"].ToString();
TextBoxDomain.Text = reader["Domain"].ToString();
TextBoxOS.Text = reader["OSname"].ToString();
TextBoxSP.Text = reader["SP"].ToString();
TextBoxMemory.Text = reader["Memory"].ToString();
TextBoxProcessor.Text = reader["Processor"].ToString();
TextBoxManufacturer.Text = reader["Manufacturer"].ToString();
TextBoxModel.Text = reader["Model"].ToString();
}
////// Rest of code goes from Here ...