SQL Insert Query Using C#

后端 未结 8 1778
长情又很酷
长情又很酷 2020-11-27 17:24

I\'m having an issue at the moment which I am trying to fix. I just tried to access a database and insert some values with the help of C#

The things I tried (worked)

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 18:02

    class Program
    {
        static void Main(string[] args)
        {
            string connetionString = null;
            SqlConnection connection;
            SqlCommand command;
            string sql = null;
    
            connetionString = "Data Source=Server Name;Initial Catalog=DataBaseName;User ID=UserID;Password=Password";
            sql = "INSERT INTO LoanRequest(idLoanRequest,RequestDate,Pickupdate,ReturnDate,EventDescription,LocationOfEvent,ApprovalComments,Quantity,Approved,EquipmentAvailable,ModifyRequest,Equipment,Requester)VALUES('5','2016-1-1','2016-2-2','2016-3-3','DescP','Loca1','Appcoment','2','true','true','true','4','5')";
            connection = new SqlConnection(connetionString);
    
            try
            {
                connection.Open();
                Console.WriteLine(" Connection Opened ");
                command = new SqlCommand(sql, connection);                
                SqlDataReader dr1 = command.ExecuteReader();         
    
                connection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can not open connection ! ");
            }
        }
    }
    

提交回复
热议问题