简体   繁体   English

在Visual Studio C#中使用datetimepicker的正确语法代码是什么?

[英]What is the proper syntax code in using datetimepicker in visual studio c#?

Can anyone tell me what is the proper syntax code in using datetimepicker that would be saved directly to my Microsoft sql 2005? 谁能告诉我使用datetimepicker并将其直接保存到我的Microsoft sql 2005中的正确语法代码是什么? I'm using visual studio 2008 c#. 我正在使用Visual Studio 2008 C#。

Here is my code: 这是我的代码:

private void button4_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=MJ-PC\\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True");
    SqlDataAdapter dad = new SqlDataAdapter();
    // SqlCommand cmd = new SqlCommand();
    // cmd.Connection = conn;
    dateTimePicker1.Format = DateTimePickerFormat.Short;

    string dateStr = Convert.ToString(dateTimePicker1.Text);
    dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec, Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area, @Mem_Date_Rec, @Cenro", conn);
    dad.InsertCommand.Parameters.Add("@School_Name", SqlDbType.VarChar).Value = textBox1.Text;
    dad.InsertCommand.Parameters.Add("@Province", SqlDbType.VarChar).Value = comboBox1.Text;
    dad.InsertCommand.Parameters.Add("@City", SqlDbType.VarChar).Value = textBox2.Text;
    dad.InsertCommand.Parameters.Add("@Brgy", SqlDbType.VarChar).Value = textBox4.Text;
    dad.InsertCommand.Parameters.Add("@Lot_Num", SqlDbType.VarChar).Value = textBox5.Text;
    dad.InsertCommand.Parameters.Add("@Area", SqlDbType.Int).Value = textBox6.Text;
    dad.InsertCommand.Parameters.Add("@Mem_Date_Rec", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
    dad.InsertCommand.Parameters.Add("@Cenro", SqlDbType.VarChar).Value = textBox8.Text;    

    conn.Open();
    dad.InsertCommand.ExecuteNonQuery();
    conn.Close();
}

The problem here is the datetimepicker, in my sql server Mem_Date_Rec is a datetime, so whenever I try to run it and save something on my database, dad.InsertCommand.ExecuteNonQuery(); 这里的问题是datetimepicker,在我的SQL Server中,Mem_Date_Rec是日期时间,因此,每当我尝试运行它并将其保存在数据库中时,dad.InsertCommand.ExecuteNonQuery(); Keeps on saying "Incorrect syntax near '@Cenro'." 一直说“'@Cenro'附近的语法不正确”。

Can anyone help me out here please, it would be a really great help. 任何人都可以在这里帮助我,这将是非常有用的帮助。

I feel like you try to insert your parameter to dad.InsertCommand command not cmd command. 我感觉像您尝试将参数插入到dad.InsertCommand命令而不是cmd命令。

dad.InsertCommand.Parameters.Add("@Mem_Date_Rec", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;

Because your dad.InsertCommand has a parameter called @Mem_Date_Rec , not cmd . 因为您的dad.InsertCommand有一个名为@Mem_Date_Rec的参数,而不是cmd I have no idea what is your cmd for exactly. 我不知道你的cmd是什么。 It's useless this case. 这种情况下没有用。 You can't add a parameter value in an SqlCommand that doesn't have any parameter definition . 您不能在没有任何参数定义的SqlCommand中添加参数值

Also use using statement to dispose your SqlConnection and SqlCommand like; 也可以使用using语句来处理SqlConnectionSqlCommand类的;

using(SqlConnection conn = new SqlConnection(ConnectionString))
using(SqlCommand cmd = conn.CreateCommand())
{
   //
}

If you want to write a proper syntax code, you need start reading a book, articles, blogs, examples etc.. 如果要编写正确的语法代码,则需要开始阅读书籍,文章,博客,示例等。

edit 编辑

You're missing something in your SQL. 您在SQL中缺少某些内容。 Change this: 更改此:

> dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools
> (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec,
> Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area,
> @Mem_Date_Rec, @Cenro", conn);

To this 对此

dad.InsertCommand = new SqlCommand("INSERT INTO tblSchools (School_Name, Province, City, Brgy, Lot_Num, Area, Mem_Date_Rec, Cenro) VALUES(@School_Name, @Province, @City, @Brgy, @Lot_Num, @Area, @Mem_Date_Rec, @Cenro)", conn);

INSERT INTO table (columns) values (value) INSERT INTO表(列)值(值)

you had: INSERT INTO table (columns) values (value 您具有:INSERT INTO表(列)值(值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM