|
Could someone clear up a possible memory leak for me? In using ADO.NET in MFC Projects it is written:
#pragma push_macro("new")
#undef new
try
{
m_OleDbConnection = new OleDbConnection(
S"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test.mdb"
);
m_OleDbConnection->Open(); // Open up the connection
m_OleDb = new OleDbCommand(S"select * from Persons", m_OleDbConnection);
m_Reader = m_OleDb->ExecuteReader();
int count = 0;
while (m_Reader->Read())
{
AddToList(count,m_Reader->get_Item("First Name")->ToString(),
m_Reader->get_Item("Last Name")->ToString(),
m_Reader->get_Item("Phone Number")->ToString()
);
count++;
}
}
catch(Exception *e)
{
AfxMessageBox(CString(e->ToString()));
}
__finally
{
m_Reader->Close();
m_OleDbConnection->Close();
}
#pragma pop_macro("new")
What is bothering me is there is a new with no delete. The most surprising one is m_OleDb which doesn’t even have a close, let alone a delete. I tried adding a close as well as a delete and neither was accepted by the compiler.
I open and close the database hundreds of times, so I wanted to consider leaving it open and then I started to think about the memory leak problem.
Can anyone give me a hint why a memory leak doesn’t seem to play here?
Thanks,
Ilan
|
|
|
|
|
You need to look up on Garbage Collection in .NET. Basically, there is an entity called the Garbage Collector (GC) that runs in a background thread. It does the cleanup for you, when it determines that a cleanup needs to be done. In your case, when the GC decides to run, if it sees that there are no live references to OleDbConnection and OleDbCommand objects, it will reclaim the memory.
In other words, you can't explicitly delete, as in C++, the GC will (attempt) do it when it decides to. The Close method is to release resources held up by the object, OleDbConnection has Close because it wraps a database connection, but OleDbCommand is just an abstraction for a SQL command, so that's why it doesn't have Close. Calling Close has nothing to do with GC, though.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
ERm.... given that his code contains ->, I'd say it's C++ and not C#
Oh, it's MC++, because he's using .NET objects.
Christian Graus - Microsoft MVP - C++
-- modified at 17:49 Tuesday 6th September, 2005
|
|
|
|
|
I have a problem: I can not get default column value in ADO.NET:
I have a sample table
create table
testtable(id int identity(1,1) not null,
notnullcolumndef5 int not null default (5))
column [notnullcolumndef5] can not be null and has a default value of 5.
now goto VStudio, I use c#, but it does not matter, generate a simple SqlDataAdapter with
select * from testtable
We will use untyped dataset. so
ourSqlDataAdapter.FillSchema(ourUntypedDataSet, SchemaType.Source, "testtable")
this statement fills testtable schema. If you look then in columns collection, our [notnullcolumndef5] column has DefaultValue property set to System.DBNull. WHY ? Where is 5 ?
I think that I simply do not understand something, as fas for such a common problem I did not find any articles. please help !
|
|
|
|
|
|
Thanks ! Right..
I have understood it already on my own. A good example is DATETIME() default - it can not be returned as value, as far as it is insetion time into DB. Of other cases, that can be server-dependent. So - it's right that the field is not filled.
|
|
|
|
|
Hello,
I want to restore an in used SQL Database(via SQL-Script), but is anyone connected with then Database, I can´t restore the Database.
Please help me!!!
|
|
|
|
|
Can you be a bit more explicit of your needs ?
|
|
|
|
|
Its easy to achieve this using SQL DMO. You can expllicitly kill all the existing connections.
But if you are using SQL script then use the following SP
se master
go
--Type procedure
--author mak mak_999@yahoo.com
--date written 4/19/2000
--project maintenance
--objective procedure to kill process for a given database
create procedure usp_killprocess @dbname varchar(128) as
set nocount on
set quoted_identifier off
declare @kill_id int
declare @query varchar(320)
declare killprocess_cursor cursor for
select a.spid from sysprocesses a join
sysdatabases b on a.dbid=b.dbid where b.name=@dbname
open killprocess_cursor
fetch next from killprocess_cursor into @kill_id
while(@@fetch_status =0)
begin
set @query = "kill "+ convert(varchar,@kill_id)
exec (@query)
fetch next from killprocess_cursor into @kill_id
end
close killprocess_cursor
deallocate killprocess_cursor
--usage
--exec usp_killprocess "mydatabasename"
Source: http://www.killersites.com/mvnforum/mvnforum/viewthread?thread=2240
|
|
|
|
|
hellow to all ..
how can i change the tabel titels in the datagrid ...
i have a tabel in the database , and it have names ...
i wana change it name's to what i wana to ..
how can i do it ?
thxx a lott ..
|
|
|
|
|
Change them in the html of the web form by specifing the header text in BoundColumn
example :
<asp:BoundColumn DataField="ded_dte" SortExpression="Date" HeaderText="Date">
<HeaderStyle Width="66px"></HeaderStyle>
</asp:BoundColumn>
-- modified at 4:56 Tuesday 6th September, 2005
|
|
|
|
|
Assuming you are using Windows DataGrid and you want to change the 4th column header
this.yourDataGridTableStyle.GridColumnStyles[3].HeaderText = "Anonymous";
|
|
|
|
|
hi!
I have 2 form and 2 sql table :
the following fields are for 2 table :
table 1 :
id_user type[int],username type[nvarchar],password type[nvarchar]
table 2:
id_name type[int],name type[nvarchar],telephone type[decimal]
//-----
after the user enter his username and password , if it's correct , the second form show the private information .
Now I want to add,delete,save the User private information , in how ,every user enter his
username and password ,show his private info .
How can it possible?
//---------------------
in btnOK_Click Event ,for affirmative user and password is Here :
*************************
private void btnOK_Click(object sender, System.EventArgs e)<br />
{<br />
if ((txtUserName.Text.Trim().Length == 0)|| (txtPassword.Text.Trim().Length == 0))<br />
{<br />
MessageBox.Show(Empty_MESSAGE,CAPTION_TITLE);<br />
txtPassword.Text="";<br />
txtUserName.Text="";<br />
return;<br />
}<br />
string strFilter;<br />
strFilter = "SELECT UserName,Password FROM User_Name WHERE Password='" +txtPassword.Text + "' AND UserName='"+txtUserName.Text+"'";<br />
userAdapter=new SqlDataAdapter(strFilter,UserConnection);<br />
SqlCommandBuilder UserCmd=new SqlCommandBuilder(userAdapter);<br />
DataTable UserTable=new DataTable();<br />
userAdapter.Fill(UserTable);<br />
UserView = new DataView(UserTable,DEFAULT_FILTER,DEFAULT_SORT,DataViewRowState.OriginalRows);<br />
if (UserView.Count == 0)<br />
{<br />
MessageBox.Show(NO_RECORDS_FOUND_MESSAGE,CAPTION_TITLE);<br />
txtPassword.Text="";<br />
txtUserName.Text="";<br />
}<br />
else<br />
{<br />
this.Hide();<br />
Private_Public_Form frm_pub_pri=new Private_Public_Form();<br />
frm_pub_pri.ShowDialog();<br />
}<br />
<br />
}<br />
***********************************************<br />
thanks in advance!!!<br />
<br />
<br />
mostafa hosseiny b.
|
|
|
|
|
manije wrote:
strFilter = "SELECT UserName,Password FROM User_Name WHERE Password='" +txtPassword.Text + "' AND UserName='"+txtUserName.Text+"'";
This is NOT how I showed you to build this query[^]. This is now vulnerable to a SQL Injection Attack, and one that is very easily prevented. Please re-read the code I sent you before and if you don't understand it, I'll be happy to answer questions.
If you want to read more about how to prevent SQL Injection attacks. Something that is very important and double importance seing as this is verifying user names and passwords then you should read SQL Injection Attacks and Tips on How to Prevent Them[^]
Finally, just as an example of how easy your code is to attack I would suggest you type the following into the password box and trace through the code to see what it does:
' OR 1=1;--
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
Hi !
thanks for your guide !
but ,I don't understand ,OR,
I want to know , How can observe a user his private information , After everyone enter his username and password .
by the way , the code I wrote,can chech , is there user in the table or not ,in any case ,please help me !
thanks again!
mostafa hosseiny b.
|
|
|
|
|
manije wrote:
but ,I don't understand ,OR,
I want to know ,
Colin has not really answered this, instead, he's pointed out that you've ignored the advice he gave you before.
manije wrote:
How can observe a user his private information
By you writing the code to display it. Once you know someone has logged in, you can ask the database for their information and display it any way you like.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
in vb.net when we create connection string
we want dsn name in the connection string i want connection via sqlserver
please suggest me what connection string i write
notify i don't want server name and database name in the connection string.
please help me
|
|
|
|
|
Since a DSN is part of ODBC configuration you need to use the ODBC data provider which will reduce your performance connecting to SQL server. And OLEDB option is to use a UDL file look under Storing Connection Strings on this page http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daag.asp[^].
Anothe option would be to encrypt the server and database name in a string and add the other information in at runtime.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
If you create a DSN called abcd,
string myConnectionString ="DSN=abcd;UID=root;PWD=root";
OdbcConnection MyConn;
OdbcCommand MyCmd = new OdbcCommand();
MyConn = new OdbcConnection(myConnectionString);
MyConn.Open();
MyCmd.Connection = MyConn;
StringBuilder SQL = new StringBuilder();
SQL.Append("SELECT ");
SQL.Append("*");
SQL.Append("FROM ");
SQL.Append("Authors");
MyCmd.CommandText = SQL.ToString();
OdbcDataReader result = MyCmd.ExecuteReader(CommandBehavior.CloseConnection);
int nResultCount = 0;
while (result.Read())
{
++nResultCount;
}
|
|
|
|
|
thanks.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
|
Hi,
I am using MySqL-FRONT As my database.I installed MySql beta server,MySQl Font and MySql Odbc driver and I started the service of MySql .But when I am connecting the MySql -Front,the system seems to be hang.The system should have enough space(128 MB RAM).Please help me to overcome this situation.
Jeeva
|
|
|
|
|
thank for your reply(Colin Angus Mackay )in advance!!
----
the type of password field = int
----
and this my code for check!?
----
private void btnOK_Click(object sender, System.EventArgs e)
{
//------->Step :1
if ((txtUserName.Text.Trim().Length == 0)|| (txtPassword.Text.Trim().Length == 0))
{
MessageBox.Show(Empty_MESSAGE,CAPTION_TITLE);
txtPassword.Text="";
txtUserName.Text="";
return;
}
//------>Step :2
string strFilter;
strFilter = "SELECT UserName,Password FROM User_Name WHERE Password='" +txtPassword.Text + "' AND UserName='"+txtUserName.Text+"'";
userAdapter=new SqlDataAdapter(strFilter,UserConnection);
SqlCommandBuilder UserCmd=new SqlCommandBuilder(userAdapter);
DataTable UserTable=new DataTable();
userAdapter.Fill(UserTable);
//------ Counting and Displaying
UserView = new DataView(UserTable,DEFAULT_FILTER,DEFAULT_SORT,DataViewRowState.OriginalRows);
//---->Step :3
if (UserView.Count == 0)
{
MessageBox.Show(NO_RECORDS_FOUND_MESSAGE,CAPTION_TITLE);
txtPassword.Text="";
txtUserName.Text="";
}
else
{
this.Hide();
//----displaying form
Private_Public_Form frm_pub_pri=new Private_Public_Form();
frm_pub_pri.ShowDialog();
}
//************* Finish ********/
}
//-----------
the difficult in this app. is , when password type=int
,system give an error , if we type in textbox of password non-numeral(character)
|
|
|
|
|
First, 15 chars for a password is too small. Normally the passwords I use are 20+ chars and it irritates me when a website cannot accept such a long password. The longer the password, the more secure it is because it takes longer to crack. You should use something like an nvarchar(128) for a password.
Also, you should consider looking at encryption routines so that the password is stored in a secure fashion.
manije wrote:
By the Way if Password type=int [length=4] , what's the result the check ?
That doesn't make any sense.
To check the password on your current set up:
SqlConnection conn = new SqlConnection(myConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT id_user FROM TheUserTable "+
"WHERE username = @username AND password = @password";
cmd.Parameters.Add("@username", myUserName);
cmd.Parameters.Add("@password", myPassword);
conn.Open();
object result = cmd.ExecuteScalar();
if (result == null)
{
}
else
{
int userID = (int)result;
}
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
hi all!
in c#.net:
in my application ,how can it check the user and password
is correct or not ?
for recieve and save user and password , we use a sql Table
thanks in advance!!
mostafa hosseiny b.
|
|
|
|
|