Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using StreamReader.ReadBlock to read a file

Status
Not open for further replies.

markknowsley

Programmer
Joined
Aug 30, 2005
Messages
152
Location
GB
I have a SQL statement that I want to add a parameter to half way through.
i.e.
select name
from table
where name = + parameter name
---------------------------------
order by name

so I want to start reading the sql into a string, then stop reading and add a value from my form, and then carry on reading the sql again.

My C# would look like
StreamReader sr = new StreamReader();
string sqlSelect = sr.ReadBlock(some values) + parameter + sr.ReadBlock(some values);

Please can someone tell me how to get the StreamReader.ReadBlock to stop at a certain point in my SQL file, and then continue to read from that point?

Maybe there's a better way of doing this...
 
Where are you looking at reading it from? If it is just in your code, you can do this:

Code:
      SqlConnection mConn = new SqlConnection("Some connection string");
      SqlCommand mCmd = new SqlCommand("Select * from Users Where Name = @Name Order By Name Desc", mConn);
      mCmd.Parameters.Add("@Name", txtUserName.Text);

I just wrote that on the fly, but that is the strategy for attacking that problem.

You can even do a string replace if you REALLY wanted to.
Code:
string mCommandText = "Select * from Users Where Name = @Name Order By Name Desc".Replace("@Name", "Guru7777");

I think what you are trying for is overkill, unless I misunderstand your goals.

Hope that helps.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Thanks for the advice, here's what I ended up doing:

string stringForename = txtForename.Text;
string stringSurname = txtSurname.Text;
string filename = Server.MapPath("sqlfile.sql");
StreamReader sr = new StreamReader(filename);
string stringSQLStatement = sr.ReadToEnd();
sr.Close();
StringBuilder sbSQLStatement = new StringBuilder(stringSQLStatement);
sbSQLStatement.Insert(1192, "'" + stringForename + "'");
int size = sbSQLStatement.Length;
sbSQLStatement.Insert(size, "'" + stringSurname + "'");
string sqlSelect = Convert.ToString(sbSQLStatement);

Then use sqlSelect to populate a datagrid (or whatever).

StringBuilder worked really well - once you know stuff like this text is really easy to deal with (I remember trying to do string - related stuff in 'C' when I was in University and tearing my hair out).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top