C# connect to mysql through user control

后端 未结 2 1650
既然无缘
既然无缘 2020-12-21 11:14

Hey Guy\'s I am new to C#.

And my question is , is it possible to connect to database(through webusercontrol).

I am trying to make list of where the value c

相关标签:
2条回答
  • 2020-12-21 11:36

    So basically, if you have a database active you should first get the data out of it.

    private static string connString = "server=127.0.0.1; userid=yourUserHere; password=youPasswordHere; database=yourDatabaseNameHere";
    public static DataTable SelectData(MySqlCommand command)
            {
                try
                {
                    DataTable dataTable = new DataTable();
    
                    using (MySqlConnection connection = new MySqlConnection())
                    {
                        connection.ConnectionString = connString;
                        connection.Open();
    
                        command.Connection = connection;
                        MySqlDataReader reader = command.ExecuteReader();
                        dataTable.Load(reader);
    
                        return dataTable;
                    }
                }
                catch (MySqlException e)
                {
                    Console.Write(e.Message);
                    return null;
                }
            }
    

    Then in the context you need to call this method with an SQL line. You should always use parameterized queries to minimize the the risk of SQL injections and such. Also you need to convert the information you have from a datatable to a list (if thats what you want). Like this:

    public List<string> dataTableToString(DataTable table)
            {
                List<string> Labels = new List<string>();
                foreach (DataRow row in table.Rows)
                {
                    //index of row you want returned in the list
                    Labels.Add(row[2].tostring())
                }
             return labels
             }
    public List<string> whateverInformationYouWantHere(string labelID,)
            {
                MySqlCommand command = new MySqlCommand();
                command.CommandText = "SELECT * FROM LABELS WHERE LabelID = @labelID";
                command.Parameters.AddWithValue("labelID", labelID);
                return dataTableToString(Databasehandler.SelectData(command));
            }
    

    Then all you have to do is make a foreach loop and insert all the label items in your UL. (If you have questions please feel free to ask).

    0 讨论(0)
  • 2020-12-21 11:46

    Just take a look at this:

    HTML:

     <div>
         <ul>
            <li><a href="#" id="sample1" runat="server">This value comes from database tabel</a></li>
            <li><a href="#">This 1 too</a></li>
            <li><a href="#">and this 1 too</a></li>
            </ul>
        </div>
    

    Code Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
       sample1.InnerText = "just imagine that this text is came from the first column from your DB";
    }
    
    0 讨论(0)
提交回复
热议问题