.net数据存储 xml读取

   SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            SqlCommand myCommand = new SqlCommand("CustomerAdd", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterFullName = new SqlParameter("@FullName", SqlDbType.NVarChar, 50);
            parameterFullName.Value = fullName;
            myCommand.Parameters.Add(parameterFullName);

            SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 50);
            parameterEmail.Value = email;
            myCommand.Parameters.Add(parameterEmail);

            SqlParameter parameterPassword = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
            parameterPassword.Value = password;
            myCommand.Parameters.Add(parameterPassword);

            SqlParameter parameterCustomerID = new SqlParameter("@CustomerID", SqlDbType.Int, 4);
            parameterCustomerID.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterCustomerID);

            try {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myConnection.Close();

                // Calculate the CustomerID using Output Param from SPROC
                int customerId = (int)parameterCustomerID.Value;

                return customerId.ToString();
            }
            catch {
                return String.Empty;
            }


  
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            SqlCommand myCommand = new SqlCommand("CustomerAdd", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SqlParameter parameterFullName = new SqlParameter("@FullName", SqlDbType.NVarChar, 50);
            parameterFullName.Value = fullName;
            myCommand.Parameters.Add(parameterFullName);

            SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 50);
            parameterEmail.Value = email;
            myCommand.Parameters.Add(parameterEmail);

            SqlParameter parameterPassword = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
            parameterPassword.Value = password;
            myCommand.Parameters.Add(parameterPassword);

            SqlParameter parameterCustomerID = new SqlParameter("@CustomerID", SqlDbType.Int, 4);
            parameterCustomerID.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterCustomerID);

            try {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                myConnection.Close();

                // Calculate the CustomerID using Output Param from SPROC
                int customerId = (int)parameterCustomerID.Value;

                return customerId.ToString();
            }
            catch {
                return String.Empty;
            }




private bool IfExists(object parameters)
        {
            string str = parameters.ToString();
            string[] param = str.Split('^');
            SqlConnection conn = new SqlConnection();
            try
            {
                conn = new SqlConnection(connectionStr);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"select * from ExchangeRate
                                     where currency=@Currency and
                                          Date between @StartDate and @EndDate";
                SqlParameter paraCurrency = new SqlParameter("@Currency", SqlDbType.NVarChar);
                paraCurrency.Value = param[0];
                cmd.Parameters.Add(paraCurrency);
                SqlParameter paraStartDate = new SqlParameter("@StartDate", SqlDbType.NVarChar);
                paraStartDate.Value = param[1];
                cmd.Parameters.Add(paraStartDate);
                SqlParameter paraEndDate = new SqlParameter("@EndDate", SqlDbType.NVarChar);
                paraEndDate.Value = param[2];
                cmd.Parameters.Add(paraEndDate);
                cmd.Connection = conn;
                conn.Open();
                object o = cmd.ExecuteScalar();
                conn.Close();
                if (o == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
 
 
private void SaveExchangeRates(System.Xml.XmlNode node, string currency)
        {
            DataTable dt = new DataTable();
            dt.TableName = "ExchangeRate";
            dt.Columns.Add("Currency", System.Type.GetType("System.String"));
            dt.Columns.Add("Date", System.Type.GetType("System.DateTime"));
            dt.Columns.Add("ExchangeRate", System.Type.GetType("System.Decimal"));
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                XmlNode childNode = node.ChildNodes[i];
                double exchangeRate = double.Parse(childNode.ChildNodes[3].InnerText) / double.Parse(childNode.ChildNodes[2].InnerText);
                DataRow dr = dt.NewRow();
                dr["Currency"] = currency;
                dr["ExchangeRate"] = exchangeRate;
                dr["Date"] = DateTime.Parse( childNode.ChildNodes[0].InnerText );
                dt.Rows.Add(dr);
            }
            StoreDate(dt);
        }
        private void StoreDate(DataTable dt)
        {
            SqlConnection conn = new SqlConnection();
            try
            {
                conn = new SqlConnection(connectionStr);
               
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"Insert into ExchangeRate(Currency,Date,ExchangeRate)
                                   values
                                    (
                                       @Currency,
                                        @Date ,
                                        @ExchangeRate
                                    )";
                cmd.Parameters.Add("@Currency",SqlDbType.NVarChar,20,"Currency");
                cmd.Parameters.Add("@ExchangeRate", SqlDbType.Decimal, 18, "ExchangeRate");
                cmd.Parameters.Add("@Date",SqlDbType.DateTime,20,"Date");
                cmd.Connection = conn;
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = cmd;
                da.Update(dt);
                conn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
 
 private bool Delete(object parameters)
        {
            string str = parameters.ToString();
            string[] param = str.Split('^');
            SqlConnection conn = new SqlConnection();
            try
            {
                conn = new SqlConnection(connectionStr);
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"delete from ExchangeRate
                                    where currency=@Currency and
                                          Date between @StartDate and @EndDate";
                SqlParameter paraCurrency = new SqlParameter("@Currency", SqlDbType.NVarChar);
                paraCurrency.Value = param[0];
                cmd.Parameters.Add(paraCurrency);
                SqlParameter paraStartDate = new SqlParameter("@StartDate", SqlDbType.NVarChar);
                paraStartDate.Value = param[1];
                cmd.Parameters.Add(paraStartDate);
                SqlParameter paraEndDate = new SqlParameter("@EndDate",SqlDbType.NVarChar);
                paraEndDate.Value = param[2];
                cmd.Parameters.Add(paraEndDate);
                cmd.Connection = conn;
                conn.Open();
                object o = cmd.ExecuteScalar();
                conn.Close();
                if (o == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
原文地址:https://www.cnblogs.com/kevinge/p/1213460.html