asp.net 点击按钮后显示数据库中的银行列表

rqdpfwrv  于 2022-11-26  发布在  .NET
关注(0)|答案(1)|浏览(134)

我有3个输入(用户名,密码和数据源)和一个按钮(连接)通过点击按钮列表上的银行在数据库将被演示

\[HttpGet\]
public JsonResult search()
{
//try  connect to make connection on sql engine
// if it could to connect get list of db and return
// that

            var username  = Request.QueryString["username"].ToString();
            var password = Request.QueryString["password"].ToString();
            var datasource = Request.QueryString["databaseConString"].ToString();
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder["Server"] = datasource;
            builder["Connect Timeout"] = 1000;
            builder["Trusted_Connection"] = true;
            builder["Integrated Security"] = false;
            builder.Password = password;
            builder.UserID = username;
    
            List<string> list = new List<string>();
            Console.WriteLine(builder.ConnectionString);
    
            using (SqlConnection con = new SqlConnection(builder.ConnectionString))
            {    
                con.Open();
                using (SqlCommand cmd = new SqlCommand(" **problem **SELECT name FROM sys.databases**"**,con))
                {
                    using (SqlDataReader dr = cmd.ExecuteReader())
                   {

// # I have problem in this loop **

                        while (dr.Read())    
                        {    
                            //list.Add(dr.GetString());
                            Console.WriteLine("{0}",dr[0]);    
                        }
                        //foreach(var item in list)
                        //{
    
                        //    Console.WriteLine(list);
                        //}                       
                    }
                    return  Json( JsonRequestBehavior.AllowGet);
                }    
            }
        }

我不知道如何使用sqlDataReader并将对象添加到我的列表中

0ve6wy6x

0ve6wy6x1#

问题从系统数据库中选择名称**

这里你还需要知道你要在数据库下的哪个表中搜索bank name,你没有指定。例如,你应该写如下:

SELECT BankName FROM BankTable [dbo].[BankDatabase]

我不知道如何使用sqlDataReader并将对象添加到我的列表中
你可以在while循环中从中获取数据库实体。最后将属性绑定到列表中。你的代码应该如下所示:

[HttpGet]
        public JsonResult search()
        {
            var username = Request.QueryString["username"].ToString();
            var password = Request.QueryString["password"].ToString();
            var datasource = Request.QueryString["databaseConString"].ToString();
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder["Server"] = datasource;
            builder["Connect Timeout"] = 1000;
            builder["Trusted_Connection"] = true;
            builder["Integrated Security"] = false;
            builder.Password = password;
            builder.UserID = username;

            
            List<string> listData = new List<string>();
            using (SqlConnection con = new SqlConnection(builder.ConnectionString))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT [BankName] FROM [DataBaseName].[dbo].[TableName]", con))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {

                        while (reader.Read())
                        {

                            string bankName = reader.GetString(0);
                            listData.Add(bankNamee);
                        }

                    }
                    return Json(listData);
                }
            }
        }

**注意:**确保reader.GetString(0)的顺序是正确的。这意味着如果[BankName]在第一个顺序b中,它应该是reader.GetString(0)。另外,还要记住数据类型。在这里,我将获取每个bankNamee并绑定到我在上面定义的listData.Add(bankNamee);列表,以供您参考
输出:

相关问题