|
pranee24 wrote: i Tried but its not working
Does not tell anyone what you tried or why you think it's not working.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
modified 12-Jul-12 14:39pm.
|
|
|
|
|
Why is your message type "General" rather than "Question"?
pranee24 wrote: i need to get the DataBase Name and File Name from TextBox
1. So you shouldn't be using string literals then, rather use string variables, load them from the textbox(es), and feed them to your SqlCommand parameters.
2. Are you sure you want the users to have the liberty to enter anything they like? When I need access to a small number of databases, I tend to offer a choice (say a ComboBox), and have basically a switch in my code.
pranee24 wrote: its not working
interesting.
|
|
|
|
|
try this:
string dbName = txtDbName.Text.Trim();
string fileName1 = txtFileName1.Text.Trim();
string fileName2 = txtFileName2.Text.Trim();
SqlConnection conn = new SqlConnect(
"Integrated Security=SSPI;Data Source=DBSRV1;Initial Catalog=master");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_attach_db";
cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = dbName;
cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value = fileName1;
cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).Value = fileName2;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Note: Assuming user sending required/valid data from the above textboxes
|
|
|
|
|
Hello members,
I am developing Asp.net website application with framework 4.0 in Visual studio 2010. I would like to convert App_Code to single DLL which contains class files and dataset. Is it possible? Please give your suggestions
Thanks
modified 13-Jul-12 1:24am.
|
|
|
|
|
yes you can make DLL file of all the classes. this will help you to protect your code from others
hope dis link will help you
http://www.c-sharpcorner.com/uploadfile/mahesh/dll12222005064058am/dll.aspx
|
|
|
|
|
Just build the web app. That will already copy it into a single dll. You don't need to do anything special.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
hello
I whould like to convert the requete execute reader to liste:help me please.
this me requete:
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("SELECT libelle_titre FROM titre", ConnectBase.getinstance().Connection());
List<int> liste= new List<int>();
affiche_titre = comm.ExecuteReader();
while (affiche_titre.Read())
{
liste.Add(Convert.ToInt32(affiche_titre["libelle_titre"].ToString()));
titre1.Text = liste[0]ToString();
titre2.Text = liste[1].ToString();
titre3.Text = liste[2].ToString();
thanks
|
|
|
|
|
Uh ... what's the question? You're already moving the results into a list.
|
|
|
|
|
my question hwo to execute my requete in the list and
any result in the texBox :
example:
..............
..................
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("SELECT Title FROM titre", ConnectBase.getinstance().Connection());
rs = comm.ExecuteReader();
while (rs.Read())
string []tab = rs.ToString()
texBox1.Text= tab[0];
texBox2.Text= tab[1];
texBox3.Text= tab[2];
......
...
Thanks
|
|
|
|
|
I hate to criticize someone who has asked for help but this anti-pattern needs to stop. I am referring to:
<br />
Convert.ToInt32(affiche_titre["libelle_titre"].ToString());<br />
Data reader values already contain the object type. Look it up from the query. As for accuracy, the code doesn't seem wrong, unless you didn't bother to open your connection. My patter for this would be:
<code>
using(SqlConnection connection = new SqlConnection(connectionString){
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandText = "your sql statement";
using(SqlDataReader reader = command.ExecuteReader()){
while(reader.Read()){
list.Add((Int32)reader[0]);
}
}
}
</code>
|
|
|
|
|
Even slightly more elegant would be:
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
using (SqlCommand command = new SqlCommand("your sql statement", connection)) {
using(SqlDataReader reader = command.ExecuteReader()){
while(reader.Read()){
list.Add((Int32)reader[0]);
}
}
}
}
as SqlCommand also implements IDisposable, and offers a constructor that takes both the SQL statement and the connection.
|
|
|
|
|
I omit the using on SqlCommand for brevity since its sole function is to close the connection. However, on the data reader, there are times when you want to re-use the command but you can't re-use a command with an open reader. This lets it be much, much easier to not have to fix that bug.
With regard to the constructor, it is matter of taste and I will never fault anyone for using it. Unfortunately, I a lot of my code is left to be maintained by others. I like to think that the brilliance in the code I write is not only in its function but in the ability to line-by-line debug without any surprises.
In my younger career I used to use stored procedures exclusively. I have now moved to embedding the SQL statments in the source files for easier version control. With this, sometimes comes complex dynamic sql statements. (Especially, when reading from SQL Server and then Selecting from Oracle, ugh) with a monstrosity like that, I really don't want to use the constructor because then you have to step into a method in a constructor to see what it does when stepping over an examining the results is so much easier.
Finally, and I never use it, don't forget the option to immediately close the command in command.ExecuteReader().
<br />
command.ExecuteReader(CommandBehavior.CloseConnection);<br />
Really, there are so many great ways to write good clean code using data readers over the hide-and-seek approach that so many ORM's use that I am surprised more developers don't just go back to the basics. I have spent more time debugging one bug in Entity Framework than in debugging every line of the code we are discussing ever. (Ok, that is hyperbole but you get my drift)
|
|
|
|
|
I mostly feel the same; I haven't used an ORM yet, I avoid adding code that isn't mine when I can get things done easily myself, as that reduces the amount of foreign things I need to know and keep track of.
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: In my younger career I used to use stored procedures exclusively. I have now
moved to embedding the SQL statments in the source files for easier version
control.
Huh?
Why doesn't putting your SQL into version control work?
|
|
|
|
|
Nice
I would like to add that you can often program against the IDbConnection and IDbCommand -interfaces here. One would only need to create a connection of the correct type, the CreateCommand factory could then be used to get a DbCommand of that type.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Yep, I have been experimenting a bit with the interface-style DB operations, and it works well most of the time. IIRC I had to use real commands to get all the metadata I wanted, things such as GetSchema.
|
|
|
|
|
hello
i have a problem in this code
i don't enjoy the result in the texBox
System.Data.SqlClient.SqlDataReader affiche_titre;
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("SELECT libelle_titre FROM titre", ConnectBase.getinstance().Connection());
ListBox list = new ListBox();
DataTable titre = new DataTable();
affiche_titre = comm.ExecuteReader();
string [] row = null;
while (affiche_titre.Read())
{
row = new string[list.Items.Count];
for (int i = 0; i < list.Items.Count; i++)
{
row[i] = list.Items[i].ToString();
}
titre1.Text = row[0];
titre2.Text = row[1];
titre3.Text = row[2];
titre4.Text = row[3];
titre5.Text = row[4];
titre6.Text = row[5];
thans
|
|
|
|
|
For this code, it appears the problem is that you've never put anything into ListBox list !! So when you try to load the row array, the value of list.Items.Count is zero! and the loop is not executed (and I'd expect the titre1.Text = row[0]; line to throw an IndexOutOfRangeException ). It seems more likely that you wanted to use affiche_titre.FieldCount to fill the row array with the values from the reader's current row. I don't see ListBox list or DataTable titre used at all here (when corrected per my assumption).
If you've edited this to a smaller example then it is going to be difficult for anyone to give you good advice based on incomplete information. (We don't need the whole application!! just all of the relevant code. If you repost with more code, please use the code "button" above the Message entry box, to format the code more readably.)
|
|
|
|
|
hi
can anybody tell me how to save data to database using visual studio 2008(c#).
in sqlserver2005.
i had created the design in web application form.
please send me the code of save button.(c#)
|
|
|
|
|
shubham salwan wrote: please send me the code of save button.(c#)
We're not in the business of providing "free code". You are the programmer, your salary, you learn to write code to earn that.
If you're stuck, you can get hints and tips here. In this case, saving data to a database is usually done using an UPDATE statement in Sql. If that doesn't make any sense, then you'd best begin by finding a good book and reading up on the topic.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
protected void btnSave_Click(object sender, EventArgs e)
{
string connString = "Data Source=ServerName;Initial Catalog=DbName;Integrated Security=True"; SqlConnection conn = new SqlConnection(connString);
conn.Open();
String sql = "INSERT INTO CustomerInfo(FirstName, LastName, Address, Order, Quantity) values ('Sai', 'Prasad', 'Hyderabad', 1, 100)";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
|
|
|
|
|
Sure that works, but it is very very bad on several levels and showing it to a newbie will only create yet another newbie who doesn't write data access code properly and then has to come back and ask why things aren't working as expected.
0) You should put the actual DB access code in a Data Access Layer, then call the API of the DAL from the UI layer.
1) You should always use a parameterized query.
2) You should handle Exceptions.
|
|
|
|
|
the thing is that i use to make web sites in visual studio..
i m trying window application form.. thatz y facing problem
|
|
|
|
|
That shouldn't matter, a proper Data Access Layer can be called from many front-ends.
|
|
|
|
|
error occured
that
the type or namespace name sqlcommand could not be found
|
|
|
|