1

我目前正在构建一个数据库字体结束,我目前被卡住了。我正在尝试获取数据源下拉列表的 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()) 时不断收到语法错误,我不明白为什么。我一直在使用这个Get SQL data 并在文本框中显示它?为读者

4

1 回答 1

0

这样做

    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 ...
于 2013-07-10T10:52:05.033 回答