本文介绍了1.以下代码出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 protected void Submit(object sender, EventArgs e){ string connectionString = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString; using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = '" + txtCustomerId.Text + "'")) { using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); cmd.Connection = con; CustomerGridView.DataSource = cmd.ExecuteNonQuery(); CustomerGridView.DataBind(); con.Close(); } }} 我尝试了什么: 1.以下代码有什么问题?What I have tried:1.What is wrong with the following code?推荐答案 string connectionString = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString; using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE CustomerId = @customer")) { using (SqlConnection con = new SqlConnection(connectionString)) { cmd.Connection = con; cmd.Parameters.Add("@customer", txtCustomerId.Text.Trim()); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); CustomerGridView.DataSource = dt; CustomerGridView.DataBind(); } } 由于 sqldataadapter [ ^ ]是已断开连接架构,无需打开和关闭手动连接。它会被照顾掉。并且由于您在使用块下使用 SqlConnection , close()处置时,$ c> part将在内部处理。 Since sqldataadapter[^] is a disconnected Architecture, No need to Open and close the connection manually. it will be taken care off. and since you are using SqlConnection under Using block, the close() part will be handled internally when the object gets disposed. 这篇关于1.以下代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 17:34