Click here to Skip to main content
15,897,132 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie20-Nov-11 12:04
CCodeNewbie20-Nov-11 12:04 
AnswerRe: Problem with SQLDataReader and Insert command Pin
Luc Pattyn20-Nov-11 12:19
sitebuilderLuc Pattyn20-Nov-11 12:19 
GeneralRe: Problem with SQLDataReader and Insert command Pin
PIEBALDconsult21-Nov-11 4:55
mvePIEBALDconsult21-Nov-11 4:55 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie21-Nov-11 8:09
CCodeNewbie21-Nov-11 8:09 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon22-Nov-11 0:37
mvePete O'Hanlon22-Nov-11 0:37 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie22-Nov-11 1:13
CCodeNewbie22-Nov-11 1:13 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon22-Nov-11 3:03
mvePete O'Hanlon22-Nov-11 3:03 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie22-Nov-11 9:50
CCodeNewbie22-Nov-11 9:50 
Hi Pete,

I'm not quite sure what you mean by
Pete O'Hanlon wrote:
I would also change your insert statement to include the SELECT COUNT as part of the where clause

but was aware of the multi-user problem. My thoughts on overcoming that issue was to split the transactions into 3 parts, Look for name > if found use the associated ID, if not found > insert name > get Autonumber ID. My code (shabby and lumpy as it may be) currently looks like
C#
try
    {
        string c = System.Environment.MachineName;
        string Domain = System.Net.Dns.GetHostName();
        ManagementObjectSearcher SerialNo = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM_ 
        Win32_SystemEnclosure");
        foreach (ManagementObject querySN in SerialNo.Get())
        {
            string SN = (querySN["SerialNumber"].ToString());
            string SysIdent = c + SN + "@" + Domain;
    
            using(SqlConnection IdentSys = new SqlConnection(some connection;connection timeout=30"));
            using(SqlCommand selectname = new SqlCommand("SELECT COUNT(1) FROM dbo.SysIdent WHERE_              
                FullName = @SysIdent", IdentSys));
            selectname.Parameters.Add("@SysIdent", SqlDbType.NVarChar, 150);
            selectname.Parameters["@SysIdent"].Value = SysIdent;
            IdentSys.Open();
            int Count = (int)(selectname.ExecuteScalar());
            if (Count == 0)
            {
                using(SqlConnection InsertSys = new SqlConnection(some other connection_;connection
                    timeout=30"));
                using(SqlCommand newname = new SqlCommand("INSERT INTO dbo.SysIdent(FullName)_
                VALUES(@FullName)", InsertSys);
                newname.Parameters.AddWithValue("@FullName", SysIdent);
                InsertSys.Open();
                newname.ExecuteNonQuery();
                InsertSys.Close();
            }
            IdentSys.Close();
            using (SqlConnection selectID = new SqlConnection(another connection;connection timeout=30");
            SqlCommand GetID = new SqlCommand("SELECT * FROM dbo.SysIdent WHERE FullName = @SysIdent", selectID);
            GetID.Parameters.Add("@SysIdent", SqlDbType.NVarChar, 150);
            GetID.Parameters["@SysIdent"].Value = SysIdent;
            selectID.Open();
            SqlDataReader GetmyID = GetID.ExecuteReader();
            while (GetmyID.Read())
            {
                int myID = (int)(GetmyID["ID"]);
                Console.WriteLine(myID);
                Console.ReadLine();
                int mySysId = myID;
                if (!EventLog.SourceExists("mylog")) EventLog.CreateEventSource("mylog", "Application");
                EventLog.WriteEntry("mylog", "ID" + mySysId + " allocated");
            }
        }
    }
    catch (Exception f)
    {
        if (!EventLog.SourceExists("mylog")) EventLog.CreateEventSource("mylog", "Application");
        EventLog.WriteEntry("mylog", "Unable to allocate SysID" + f.ToString());
    }


What are your thoughts?

On more-or-less the same issue, given that the end product will be a windows service, what would be the best way to ensure that the ID that is given on the first run remains permanently static. i.e, it will use the same ID when it runs timed_event_1 every 12 hours and the same ID for timed_event_2 which runs every 3 minutes?
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon22-Nov-11 10:20
mvePete O'Hanlon22-Nov-11 10:20 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie22-Nov-11 11:43
CCodeNewbie22-Nov-11 11:43 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon22-Nov-11 22:51
mvePete O'Hanlon22-Nov-11 22:51 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie22-Nov-11 23:06
CCodeNewbie22-Nov-11 23:06 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon23-Nov-11 0:53
mvePete O'Hanlon23-Nov-11 0:53 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie23-Nov-11 1:12
CCodeNewbie23-Nov-11 1:12 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon23-Nov-11 1:22
mvePete O'Hanlon23-Nov-11 1:22 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie23-Nov-11 10:19
CCodeNewbie23-Nov-11 10:19 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon23-Nov-11 23:44
mvePete O'Hanlon23-Nov-11 23:44 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie24-Nov-11 9:55
CCodeNewbie24-Nov-11 9:55 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon24-Nov-11 22:30
mvePete O'Hanlon24-Nov-11 22:30 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie24-Nov-11 22:32
CCodeNewbie24-Nov-11 22:32 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon24-Nov-11 22:48
mvePete O'Hanlon24-Nov-11 22:48 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie24-Nov-11 22:51
CCodeNewbie24-Nov-11 22:51 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie25-Nov-11 10:51
CCodeNewbie25-Nov-11 10:51 
GeneralRe: Problem with SQLDataReader and Insert command Pin
Pete O'Hanlon30-Nov-11 5:34
mvePete O'Hanlon30-Nov-11 5:34 
GeneralRe: Problem with SQLDataReader and Insert command Pin
CCodeNewbie30-Nov-11 8:55
CCodeNewbie30-Nov-11 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.