如何从C#程序SQL INSERT? [英] How to SQL INSERT from C# program?

查看:51
本文介绍了如何从C#程序SQL INSERT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有表NN(id,name,date,time)



我想使用SQL INSERT命令在SQL Server中插入数据。



其中

id - >是一个自动增量编号,没有文本框

name - >是在一个文本框中

日期--->今天日期也没有文字框

时间 - >是当前驯服而且没有文字框对于它



我可以在C#中发出SQL INSERT命令的语句集是什么?

解决方案

< blockquote>这应该给你你需要的东西:



点击我! [ ^ ]



这个:



也点击我! [ ^ ]


我建​​议更改定义您的数据库,因此日期和时间在Data Ty的一列中pe DateTime。通常更容易将日期和时间放在一列中。





 尝试 
{
使用(SqlConnection cn = new SqlConnection( 你的connectionString在这里))
使用(SqlCommand cmd = new SqlCommand( @ 插入NN(name,mydatetime)值(@ name,GETDATE());,cn))
{
cmd.Parameters。 AddWithValue( name,txtName.text);
cmd.ExecuteNonQuery();
}
}
catch (例外情况)
{
// 您的错误处理代码在这里
}


For example, I have table NN (id ,name,date,time)

I want to insert the data in SQL Server by using an SQL INSERT command.

where
id--> is an auto increment number and there is no text box for it
name--> is in a text box
date--->today date and also no text box for it
time-->is current tame and there is no text box for it

What is set of statements that I can in C# to issue an SQL INSERT command?

解决方案

This should give you what you need:

Click me![^]

and this:

Click me too![^]


I suggest changing the definition of your database so Date and Time are in one column of Data Type DateTime. It usually is easier to have the date and the time together in one column.


try
{
using (SqlConnection cn = new SqlConnection( "Your connectionString goes here"))
     using (SqlCommand cmd = new SqlCommand(@"Insert into NN(name,mydatetime) Values(@name,GETDATE());",cn) )
     {
         cmd.Parameters.AddWithValue ("name",txtName.text);
         cmd.ExecuteNonQuery();
      }
 }
 catch ( Exception ex)
     {
     // Your error handling code goes here
     }


这篇关于如何从C#程序SQL INSERT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆