Pergunta

there is an error red underline on (connectionString)which said that the name'ConnectionString' does not exist in the current context. Do i need to declare sth before i use the using statement?

SqlConnection conn = new SqlConnection("Data Source=baaa;Initial Catalog=InventorySystem;Integrated Security=True") ; 

    using (SqlConnection connection = new SqlConnection(connectionString)
    {
        connection.Open();

        using (SqlCommand command = new SqlCommand(
        "SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount," +
        "Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint " +
        "FROM Product, Monthlysales " +
        "where Product.P_ID = Monthlysales.P_ID AND Product.P_ID =@P_ID AND Monthlysales.Month =@Month ", connection))
        {

            command.Parameters.Add(new SqlParameter("P_ID", pid));
            command.Parameters.Add(new SqlParameter("Month", Startmonth));
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            // now you have the data in Dataset ds
        }
Foi útil?

Solução

In using statement, you want to get value of connectionString. But there is not any variable.

Change

SqlConnection conn = new SqlConnection("Data Source=baaa;Initial Catalog=InventorySystem;Integrated Security=True") ; 

to

string connectionString= "Data Source=baaa;Initial Catalog=InventorySystem;Integrated Security=True"; 

Also, you have not ending bracket in using statement.

 using (SqlConnection connection = new SqlConnection(connectionString))

Outras dicas

As your code

SqlConnection conn = new SqlConnection("Data Source=baaa;Initial Catalog=InventorySystem;Integrated Security=True") ; 

You are making instance of connection that is not connectionstring. make it String connectionString and pass them as parameter to SqlConnection.

If you are completely sure about your connectionString, user this code :

 using (SqlConnection connection = new SqlConnection("Data Source=baaa;Initial Catalog=InventorySystem;Integrated Security=True"))
    {
        connection.Open();

        using (SqlCommand command = new SqlCommand(
        "SELECT product.P_ID, Product.P_Name,Product.Leadtime, Product.SafetyStockamount," +
        "Monthlysales.Month, Monthlysales.totalsalesamount, (totalsalesamount/30) as Averagedailysales, ((totalsalesamount/30) * Leadtime + SafetyStockamount) as reorderpoint " +
        "FROM Product, Monthlysales " +
        "where Product.P_ID = Monthlysales.P_ID AND Product.P_ID =@P_ID AND Monthlysales.Month =@Month ", connection))
        {

            command.Parameters.Add(new SqlParameter("P_ID", pid));
            command.Parameters.Add(new SqlParameter("Month", Startmonth));
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top