简体   繁体   English

如何使用asp.net/c#从数据库连接数据并将其发送到字符串?

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

I need to concatenate some data from my database and send it to a string. 我需要连接数据库中的一些数据并将其发送到字符串。 My string, in turn, will send it to a code on arduino and it will light up some LEDs according to the [Line] , [Location] and [Qtd] . 反过来,我的字符串会将其发送到arduino上的代码,并根据[Line][Location][Qtd]点亮一些LED。 My database consists of: 我的数据库包括:

[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]

In my Arduino I'll need a string concatenated like this: HIK01C01LL101 . 在我的Arduino中,我需要一个这样的字符串: HIK01C01LL101

Maybe I'll need to use a for in Arduino code to turn on all LEDs that I want because I'll have more than one [Location] for my [TrackCode] . 也许我需要在Arduino代码中使用for来打开我想要的所有LED,因为我的[TrackCode]有多个[Location] [TrackCode]

Is this right? 这是正确的吗?

My Asp.NET code: 我的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>

My C# code: 我的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).
        }
    }
}

How can I do this? 我怎样才能做到这一点?

This is the query you need 这是您需要的查询

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

I hope this will help 我希望这个能帮上忙

UPDATE -- 更新-

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