2010年2月5日 星期五

C# SQL INSERT


SqlConnection conn = new SqlConnection("Data Source=*****;Initial Catalog=*****;Integrated Security=True");
conn.Open();
string sqlIns = "INSERT INTO LAN_Snmp_Value([IP], [InOctets],[OutOctets],[TotalOctets],[Time]) VALUES (@ip, @InOctets, @OutOctets,@TotalOctets,@Time)";
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, conn);
cmdIns.Parameters.Add("@ip", argv[0]);
cmdIns.Parameters.Add("@InOctets", Global.TrafficNew[0]);
cmdIns.Parameters.Add("@OutOctets", Global.TrafficNew[1]);
cmdIns.Parameters.Add("@TotalOctets", Global.TrafficNew[2]);
cmdIns.Parameters.Add("@Time", DateTime.Now);
cmdIns.ExecuteNonQuery();

cmdIns.Parameters.Clear();
cmdIns.CommandText = "SELECT @@IDENTITY";

// Get the last inserted id.
int insertID = Convert.ToInt32(cmdIns.ExecuteScalar());

cmdIns.Dispose();
cmdIns = null;
}
catch (Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
finally
{
conn.Close();
}

沒有留言:

張貼留言