我正在尝试通过在文本框中键入内容来搜索商品代码,我想从SQL数据库中获取同一行中的特定列。我已经在datagridview中创建了带有标题的列,并且我希望在特定的datagridview列中搜索数据。我已经编写了代码,但是没有用。我不知道该怎么做。我是新手,正在尝试学习vb。请提出一些建议。
这是我的代码:
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Using cn As New SqlConnection("server= PANKAJ\SQLEXPRESS; database = pankaj billing software; integrated security=true")
Using cmd2 As New SqlCommand("select itemcode As 'Item Code', item,qty As Quantity, weight as Weight from stockdata Where itemcode = @itemcode;", cn)
cmd2.Parameters.AddWithValue("@itemcode", TextBox1.Text)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
DataGridView1.DataSource = dt
For Each row As DataGridViewRow In DataGridView1.Rows
cmd2.Parameters.Add("@item", SqlDbType.VarChar)
cmd2.Parameters.Add("@qty", SqlDbType.VarChar)
cmd2.Parameters.Add("@weight", SqlDbType.VarChar)
With cmd2
row.Cells(1).Value = .Parameters("@item").Value
row.Cells(2).Value = .Parameters("@qty").Value
row.Cells(2).Value = .Parameters("@weight").Value
End With
cmd2.ExecuteNonQuery()
Next
End Using
End Using
End Sub
答案 0 :(得分:1)
我使用了位于DataGridView外部的TextBox输入要搜索的项目代码。我添加了一个Button来进行搜索并将数据检索到DataReader。
然后,DataReader加载声明为表单级别(类级别变量)的DataTable。我们希望每次搜索时都使用相同的DataTable,以便将项目添加到网格中。如果DataTable已经包含行,则使用Load方法,来自数据源的传入数据将与现有行合并。然后将DataTable绑定到DataGridView。每次用户在文本框中输入商品代码并单击搜索按钮,都会在网格中添加新行。
为了使外观更好看,列标题在Select语句中用作别名。 databse列名称后面的As子句是别名,它将作为标题显示在DataGridView中。如果您的别名中有空格,则必须将其用单引号引起来。
Private dt As DataTable
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt = New DataTable()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Using cn As New SqlConnection("server= PANKAJ\SQLEXPRESS; database = pankaj billing software; integrated security=true")
Using cmd2 As New SqlCommand("select itemcode As 'Item Code', item,qty As Quantity, weight as Weight from stockdata Where itemcode = @itemcode;", cn)
cmd2.Parameters.AddWithValue("@itemcode", txtItemCode.Text)
cn.Open()
Dim dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
DataGridView1.DataSource = dt
End Using
End Using
End Sub
P.S。很高兴看到参数的使用和Using块! :-)
答案 1 :(得分:0)
我看到了几件事要检查:
[]
或双引号"
)而不是单引号('
)。AddWithValue()
函数,而应使用确切的列类型和长度调用Add()
。dt
的声明位置。将所有内容放在一起(猜测列名称/长度,并且尚未更改数据库名称),您将得到以下信息:
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Dim sql = "select itemcode As [Item Code], item, qty As Quantity, weight as Weight from stockdata Where itemcode = @itemcode;"
Dim dt As New DataTable()
Using cn As New SqlConnection("server=PANKAJ\SQLEXPRESS;database=pankaj billing software;integrated security=true"), _
cmd2 As New SqlCommand(sql, cn)
cmd2.Parameters.Add("@itemcode", SqlDbType.NVarChar, 10).Value = TextBox1.Text
cn.Open()
Using dr As SqlDataReader = cmd2.ExecuteReader()
dt.Load(dr)
End Using
End Using
DataGridView1.DataSource = dt
End Sub
但是重要的是,从不关于代码的足够信息。您实际上看到了什么与您期望的不同?如果出现错误消息,它到底在说什么?
答案 2 :(得分:0)
这是我的脚本代码:
protected void search_click(object sender, EventArgs e)
{
SqlConnection sqlCon = new SqlConnection(conn);
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand cmd = new SqlCommand("spSearchUser", sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", "username");
cmd.Parameters.AddWithValue("@Username", txtSearch.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
searchBind();
GridView1.Visible = true;
txtSearch.Text = "";
}
}
这是WebForm代码:
<div>
<table>
<tr>
<td>Search</td>
<td>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Text="Go" onclick="search_click" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
这是我的存储过程:
alter procedure spSearchUser
(
@SearchTerm nvarchar(50),
@Username nvarchar(50)
)
as
begin
set nocount on;
if @SearchTerm = 'username'
begin
select Username,City,Gender,Email from tblRegistration where Username
LIKE '%' + @Username + '%'
end
end
答案 3 :(得分:-1)