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,
Loading
Posted Jun 21, 2012, 12:05 PM
Raja KhouryPosted Jun 21, 2012, 4:56 AM
Now this is How it looks from the Windows form Application:
- My form debuggs without a problem but I want to make sure theconnection string is working. I have also added DATABASE connection from the server explorer window to the databse.
How I can be sure that the connection is ok to before I proceed to the next step in writting the command code.
Thanks alot,
Posted Jun 21, 2012, 12:48 AM
If you're using trustedconnection then no need to pass the user name and password. It will use current windows context (apppoolidentiy in web app).
To know more about connectionstring, refer http://www.connectionstrings.com/ site.
To ensure the connectionstring is correct or not, just establish a connection with sql server, review state and close it.
SQLConnection con = new SQLConnection("your connectionsting goes here");
con.Open()
//con.State;
Con.Close();
Or else, use sql server management studio to test it.
If you want to exact match the your query is fine. If you want to use like query then use the below query.
string sCommand = "SELECT definition FROM Dictionnary WHERE word like '%"+ TextBox1.Text.Trim()+"%'",Sqlcon;
Hope this helps you.