Monday, March 12, 2012

Manipulating SQL server databases dynamically

Hi friends,

I have problem in sending my T-SQL statements, which i generate dynamically with the help of "user entered attributes", to SQl Server.

I need my T-SQL statements to get passed to the SQl Server ,which i enter in a "Rich text Box " in my appication that i develop using Vc# .net 2005,when i click a button which i placed in my "winform".And the queries Should be executed exectly as it is, and i should get the output back in my Winform...

But,Make sure that the queries are being obtained from a rich text box placed in the Winform...

So, please assist me regarding this issue...

R.Rajaraman

Hi,

Here is an example:

string query = txt.Text;

string connStr = "";//your connection string..

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);

System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();

cmd.CommandText = query;

cmd.CommandType = CommandType.Text;

System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();

//continue..

Another option is to use SqlDataAdapter that will fill a dataset. If you need help with that let me know.

Regards,

|||

hi

i found your answer very usefull .....

But ,i need the queries directly to be passed from my rich text box control that i use in my winform....

please help me with this issue...

|||

Hi,

Perhaps I don't undestand what you mean. What do you mean directly?

If I got it right it is when you click the button that the query should get executed.

If so you should add the sample i sumbitted to the onclick event.

Regards,

|||

hi,

Thanks for the help guy... actually what i want to be done is as follows..

"I am going to enter some T-SQL statements in my Rich text Box that i placed in my form.And, i have a button named "Execute" in my win form. If I click on that button, the entire set of statements placed in the rich text box should get passed to SQL Server Query analyzer and my query batch should get executed.And ,i have to get back the results in my winform itself back..."

In Short, i need the functionality of a "Query Analyzer"..

Could you please help me with this issue......

|||

Hi,

First let me say that way you execute a query in the query analyzer you get for each statement (select/insert and ect.) a table. You can look at it as dataSet.

So, you need to fill a dataset with the query result and display it. In order to do that you first need to use the SqlDataAdapter and set the commands for it. Then you can use the fill method in the adapter to fill the result. An other option that you can try is using the data application block (or enterprise libraries) and you will get a method ExecuteDataSet(...).

Let me know if that helps.

Regards,

|||

This thread was moved to this forum (SQL Server Data Access) as the topic is more relevant here. The forum where it originally was posted (.NET Framewotk Inside SQL Server) deals with writing and running .NET code inside SQL Server (stored procs, funtions etc).

Niels

No comments:

Post a Comment