简体   繁体   中英

Show SQL Server table content in C# console app

I am trying to show some content from my SQL Server database in my C# console app.

So far I have this class :

public SqlConnection connection()
{
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "DESKTOP-UPVVOJP";
    builder.InitialCatalog = "Lagersystem";
    builder.IntegratedSecurity = true;

    return new SqlConnection(builder.ToString());
}

Product model class :

public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public int ProductStock { get; set; }
    public int ProductCategoryID { get; set; }
    public int ProductEmployeeID { get; set; }
    public DateTime ProductCreatedDate { get; set; }

    // Constructor
    public Product(string productname, int productstock, int productcategoryid, int productemployeeid)
    {
        ProductName = productname;
        ProductStock = productstock;
        ProductCategoryID = productcategoryid;
        ProductEmployeeID = productemployeeid;
        ProductCreatedDate = DateTime.Now;
    }
}

Display products method in program :

static void DisplayProducts()
{
    List<Product> products = new List<Product>();
    Database db = new Database();
    SqlConnection conn = db.connection();
    conn.Open();

    using SqlCommand command = new SqlCommand(@"SELECT * FROM Products", conn);
    {
        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            string productname = reader.GetString(1);
            int productstock = reader.GetInt32(2);
            int productcategoryid = reader.GetInt32(3);
            int productemployeeid = reader.GetInt32(4);

            products.Add(new Product() { ProductName = productname, ProductStock = productstock, 
                ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });
        }
    }

    foreach (Product product in products)
    {
        Console.WriteLine(product);
    }
}

and calling it from my main method :

static void Main(string[] args)
{
    DisplayProducts();
}      

In my DisplayProducts method, I get an error from doing this :

products.Add(new Product() { ProductName = productname, ProductStock = productstock, 
                        ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });

The error is:

There is no argument given that corresponds to the required formal parameter 'productname' of 'Product.product(string, int, int, int)'

I really don't know what that means, been googling and searching but found nothing so far.

It means that the constructor of Product need four arguments and you provide none.

Here's the signature of your constructor :

 // Constructor
    public Product(string productname, int productstock, int productcategoryid, int productemployeeid)

Change the line

products.Add(new Product() { ProductName = productname, ProductStock = productstock, 
                ProductCategoryID = productcategoryid, ProductEmployeeID = productemployeeid });

by this and you'll be fine

products.Add(new Product( productname, productstock, productcategoryid, productemployeeid));
  1. When you do a select * from products, does the query return any result ?

You also need to start from 0, hence try to put reader.GetString(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM