簡體   English   中英

如何使用asp.net/c#從數據庫連接數據並將其發送到字符串?

[英]How to concat data from database and send it to a string using asp.net/c#?

我需要連接數據庫中的一些數據並將其發送到字符串。 反過來,我的字符串會將其發送到arduino上的代碼,並根據[Line][Location][Qtd]點亮一些LED。 我的數據庫包括:

[TrackCode]     [Pn]     [Location]     [Line]      [Qtd]
---------------------------------------------------------
[GP6JLG2]     [JNKWD]   [HI-K01-A01]    [LL1]       [01]
[GP6JLG2]     [RDJJH]   [HI-K01-A02]    [LL1]       [03]
[GP6JLG2]     [1H92N]   [HI-K01-A03]    [LL1]       [03]
[GP6JLG2]     [CMNM0]   [HI-K01-B01]    [LL1]       [05]
[GP6JLG2]     [VCYYW]   [HI-K01-B02]    [LL1]       [01]
[GP6JLG2]     [K3Y7X]   [HI-K01-B03]    [LL1]       [01]
[GP6JLG2]     [329N0]   [HI-K01-C01]    [LL1]       [11]
[GP6JLG2]     [R37F7]   [HI-K01-C02]    [LL1]       [02]
[GP6JLG2]     [3659V]   [HI-K01-C03]    [LL1]       [09]

在我的Arduino中,我需要一個這樣的字符串: HIK01C01LL101

也許我需要在Arduino代碼中使用for來打開我想要的所有LED,因為我的[TrackCode]有多個[Location] [TrackCode]

這是正確的嗎?

我的Asp.NET代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="Search.Search" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            color: #3366FF;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">

        <h1><span class="auto-style1"><strong>LED TEST</strong></span></h1>
        <br />
        <asp:TextBox ID="TextBox1" runat="server" style="height: 22px; width: 128px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="text-align: center; height: 26px; width: 56px" Text="Read" />

        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ItemPosition" Height="133px" Width="198px">
            <Columns>
                <asp:BoundField DataField="TrackCode" HeaderText="TrackCode" SortExpression="TrackCode" />
                <asp:BoundField DataField="Pn" HeaderText="Pn" SortExpression="Pn" />
                <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
                <asp:BoundField DataField="Line" HeaderText="Line" SortExpression="Line" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="ItemPosition" runat="server" ConnectionString="<%$ ConnectionStrings:TravelerConnectionString %>" SelectCommand="SELECT [TrackCode], [Pn], [Location], [Line] FROM [ItemPosition] WHERE ([TrackCode] = @TrackCode)">
            <SelectParameters>
                <asp:FormParameter FormField="TextBox1" Name="TrackCode" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

我的C#代碼:

using System;
using System.Data.SqlClient;
using System.IO.Ports;

namespace Search
{
    public partial class Search : System.Web.UI.Page
    {
        SerialPort ardo;

        protected void Page_Load(object sender, EventArgs e)
        {
            ardo = new SerialPort();
            ardo.PortName = "COM3";
            ardo.BaudRate = 9600;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ardo.Open();
            ardo.Write(TextBox1.Text);
            ardo.Close();

            //At here I need to use the information from "TextBox1.Text" (always a TrackCode), access my database and return only the columns containing some datas.
           //In this case, I just need the following columns: [Line], [Location], [Qtd] (Obviously, concatenated how I showed in Note1 above).
        }
    }
}

我怎樣才能做到這一點?

這是您需要的查詢

select replace(Location, '-', '') + Line + Qtd  from table

我希望這個能幫上忙

更新-

using(SqlConnection connection = new SqlConnection("Your connection string"))
{
    string query = "select replace(Location, '-', '') + Line + Qtd  from table";
    SqlCommand command = new SqlCommand(query, connection);
    connection.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
            .....
    }
    connection.Close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM