Sunday, February 19, 2012

C# and SQL Problems

Hi, I am trying to create my first SQL Database, and figured I'd post because I am having all sorts of problems. I've read a LOT about SQL, but it doesn't appear to be helping me...lol

1) All the books and articles I read suggested using integrated security, so the user would log into the SQL Database using their computer name, etc. I did this, but I cannot seem to log in on a consistent basis (Perhaps this is why every commercial application I have seen uses the sa login?) Can I change my database back to the sa login? How would I do this?

2) I would like to only load certain columns into a dataGridView depending on what is contained in a certain column of the rows. I would also like to reorder the columns. From what I gather, my code SHOULD work, but it is not. I don't know what the issue is, and would greatly appreciate some assistance. I am also trying to rename the columns in this code as you can see, however it appears this code is doing absolutely nothing... lol

Thank you very much.

Code Snippet

private void collectionDataGridViewX_Validated(object sender, EventArgs e)
{
//Connect to the database for Collections Table
SqlConnection thisConnection = new SqlConnection(@."Server=(local)\sqlexpress;Integrated Security=True;" +
"Database=DB");

//Create DataAdapter Object
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT Number AS [#], Image1 AS [Image], Title1 AS [Title], Issue1 AS [Issue] FROM Table1 WHERE Tab = 'Tab1'", thisConnection);

//Create DataSet to contain related data tables, rows, and columns
DataSet thisDataSet = new DataSet();

//Fill DataSet using query defined previously for DataAdapter
thisAdapter.Fill(thisDataSet, "Collection");
foreach (DataRow theRow in thisDataSet.Tables["Collection"].Rows)
{
Console.WriteLine(theRow["ArtNumber"] + "\t" +
theRow["Image1"] + "\t" +
theRow["Title1"] + "\t" +
theRow["Issue1"]);
}

//Close Connection
thisConnection.Close();
}


It sounds like your trying to run without having spend any effort learning to crawl. I suggest going back to the basics.

Try these resourses for a quick 'pick me up'...

http://msdn.microsoft.com/vstudio/express/sql/learning/default.aspx#1

|||Thank you very much for the link, I will take a look. I did read Beginning SQL front to back, the applicable sections of Visual C# Step by Step, the applicable sections of Beginning Visual C# 2005, and the applicable sections of Windows Forms 2.0 Programing. I also read a great deal of online articles and examples. I just cannot get this to work, and my code looks identical to some of the examples I have found, so I don't know why it isn't working...

Thank you.

|||

I suggest that you may find using a TRY...CATCH block helpful in isolating the errors. ( I do suspect an error is occurring and you can't see it...)

For example, your code ask for ["ArtNumber"], yet your query retrieves a column named [Number] and renames it to be [#]. There doesn't seem to be a column with the name [ArtNumber] returne by the dataadapter.

|||Thank you, that was a typo when I was entering it into the forums, sorry. I am watching the videos on that link, and they are quite good. I still don't understand though, how I can sometimes log into the database, and othertimes have the login fail? It doesn't make sense to me. How can I change it from Integrated Security to my own login info? Also, I ran the SQL Query in Microsoft SQL Server Management Studio, and it works fine, but when I run it in my application nothing happens? I don't understand this. So far in the videos, I have done everything that they have shown, and they have working databases, yet I can't even get into mine consistently...

No comments:

Post a Comment