Forum Thread: C# - Connection String to Sql and Datareader/Query in vs 2010

I am new to C# and VB, I am working on a personal project but facing challenges with the coding. I have read many articles on C# & Sql coding etc.. but when it comes to creating the application there's always an error if you don't know well the logic behind it. I need some sort of guidance if possible.
The idea is to create a windows app form (C#) where there will be a textbox1 ( user inputs word) ; a search button and textbox2 ( display of query result). It's a virtual search engine/ dictionary that uses SQL database as source of information. This is what I have reached so far;

1 - Created a table ( SQL server) with Two fields ( Word ; Definition) and added around 200 words in it. Shall I create a relationship diagram to the table I created and set 'word' as primary key? Or it is not necessary?

2- Establish a connection string in VB (c#) ;
using System.Data.Sql;
using System.Data.SqlClient;

SqlConnection myConnection = new SqlConnection("user id=userid;" + "password=validpwd;server=localhost;" + "Trusted-connection =yes;" + database=main; "+ connection timeout=30");

If it's a windows authentication log in SQl can I remove the userid and password? How Can I verify the connection is working?

3- under Click-Button event

private void button1Click(object sender, EventArgs e)
{
string connectionString = "Initial Catalog=main;Data Source=(local);Integrated Security=SSPI;";
SqlConnection cn = new SqlConnection(connectionString);
string sCommand = "SELECT definition FROM Dictionnary WHERE word ='"+ TextBox1.Text.Trim()+"'",Sqlcon;

4 - What is the simplest solution for such a query? take the input text in textbox1 then search the SQl table then display it in textbox2?

I am afraid that the problem might lie in setting up the table data in SQL that's why I had connection errors, is my progress logical?

Thanks alot,

Be the First to Respond

Share Your Thoughts