List Info

Thread: C-Sharp (C#) Group: Update colum question




C-Sharp (C#) Group: Update colum question
user name
2006-06-05 19:42:48
Hi,

I want to update the "read_status" colum when
reading data, this is my
way as below:

//
------------------------------------------------------------
-----------------------------------------
string strConn = "server=" + server +
";database=" + database + ";uid="
+ uid + ";pwd=" + pwd;
SqlConnection sqlConn = null;
SqlConnection updateConn = null;

try {
	sqlConn = new SqlConnection(strConn);
	sqlConn.Open();
	SqlCommand sqlCmd = sqlConn.CreateCommand();
	sqlCmd.CommandTimeout = 10000000;
	string sql = "Select id, name From member Where
read_status = 0";
	sqlCmd.CommandText = sql;
	SqlDataReader dataReader = sqlCmd.ExecuteReader();

	updateConn = new SqlConnection(strConn);
	updateConn.Open();
	SqlCommand updateCmd = updateConn.CreateCommand();
	updateCmd.CommandTimeout = 10000000;

	while(dataReader.Read()){
		// id
		long id = Convert.ToInt64(dataReader["id"]);
		string name = dataReader["name"].ToString();

		// update read status
		// * the application will stop to here *
		updateCmd.CommandText = "Update member Set
read_status=1 Where id=" +
id;
		updateCmd.ExecuteNonQuery();
	}
}
catch(Exception exp) {
	MessageBox.Show("Error:" + exp.Message,
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
} finally {
	// close
	if(sqlConn != null)
		sqlConn.Close();
	if(updateConn != null)
		updateConn.Close();
}
//
------------------------------------------------------------
-----------------------------------------

When running the "update read status" line, the
app will be stopped.
What's the matter with my code? Thanks a lot.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Update colum question
user name
2006-06-06 20:39:53
pls testing this code ;)
datareader is only one connection using and my foragain
language bad 
sory


string strConn = "server=" + server +
";database=" + database +
";uid="+ uid + ";pwd=" + pwd;
SqlConnection sqlConn = null;
SqlConnection updateConn = null;


try
{
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
SqlCommand sqlCmd = sqlConn.CreateCommand();
sqlCmd.CommandTimeout = 10000000;
string sql = "Select id, name From member Where
read_status = 0";
sqlCmd.CommandText = sql;
SqlDataReader dataReader = sqlCmd.ExecuteReader();


updateConn = new SqlConnection(strConn);
updateConn.Open();
SqlCommand updateCmd = updateConn.CreateCommand();
updateCmd.CommandTimeout = 10000000;


if (sqlConn.State="Open")
{
sqlConn.Close();
sqlConn.Open();
}
else
{
sqlConn.Open();
}

while(dataReader.Read())
{
// id
long id = Convert.ToInt64(dataReader["id"]);
string name = dataReader["name"].ToString();


// update read status
// * the application will stop to here *
updateCmd.CommandText = "Update member Set
read_status=1 Where id=" +
id;
updateCmd.ExecuteNonQuery();
}
if (sqlConn.State="Open")
{
sqlConn.Close();
sqlConn.Open();
}
else
{
sqlConn.Open();
}

}


catch(Exception exp)
{
MessageBox.Show("Error:" + exp.Message,
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);

}
finally
{


// close
if(sqlConn != null)
sqlConn.Close(); 
if(updateConn != null) 
updateConn.Close(); 

}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )