locked
Sql server select statement and problem reading table in database. RRS feed

  • Question

  • User-424182210 posted
     string connectionString = @"Server = (localdb)\MSSQLLocalDB; connect timeout=30; database=Database1; Integrated Security = false; ";
    
    
                SqlConnection connection = new SqlConnection();
                
                    connection.ConnectionString = connectionString;
    
                    try
                    {
                        connection.Open();
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine(err.ToString());
                    }
                
    
    
    
    
    
    
                try
                {
                    SqlDataReader myReader = null;
    
                    SqlCommand myCommand2 = new SqlCommand(
                    "SELECT * from [uname];", connection);
    
    //                SqlCommand myCommand2 = new SqlCommand(
    //                 "SELECT uname  FROM dbo.Table.sql;", connection);
    
    
                    myReader = myCommand2.ExecuteReader();
                    while (myReader.Read())
                    {
                        TextBox1.Text = myReader["uname"].ToString();
                    }
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                }
    
    
    

    I am having trouble accessing a table in sql server.

    I have managed to create a table in users table with entries id and uname.

    I keep getting object not a valid name. for users table.

    Using command line I have managed to add a entry to the table.

    But when using select I get the same error.

    Wednesday, February 10, 2016 12:26 AM

Answers

  • User364663285 posted

    Try to put this

    	<connectionStrings>
    		<add name="conn2" connectionString="Data Source=192.168.?.?\your_db_instance;Initial Catalog=your_db_schema;Integrated Security=False;User ID=your_db_login;Password=??????" providerName="System.Data.SqlClient"/>
    	</connectionStrings>
    



    within Web.config/app.config file in your project, to ensure the project will work fine with the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 5:48 AM

All replies

  • User-1716253493 posted

    Do you mean?

    "SELECT * from [users];", connection);

    Wednesday, February 10, 2016 12:42 AM
  • User-698989805 posted

    The query should be like "SELECT ColumnName FROM TableName";

    Then in the reader: TextBox1.Text = myReader["ColumnName"].ToString();

    Wednesday, February 10, 2016 8:30 AM
  • User-424182210 posted

    I found the problem.

    I was using my database in servers and not sql servers.

    Wednesday, February 10, 2016 11:00 PM
  • User364663285 posted

    Try to put this

    	<connectionStrings>
    		<add name="conn2" connectionString="Data Source=192.168.?.?\your_db_instance;Initial Catalog=your_db_schema;Integrated Security=False;User ID=your_db_login;Password=??????" providerName="System.Data.SqlClient"/>
    	</connectionStrings>
    



    within Web.config/app.config file in your project, to ensure the project will work fine with the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 5:48 AM