我用的C#连接SQL server数据库,数据库连接上了,测试连接也成功,
可是就是无法将数据存放进数据库的表,程序也没报错,弄了好久还是不行,
求指点一下,接触C#和数据库不久,仿照着做点设计。这个是把串口收到的数据保存到
数据库,用的是Time控件,每隔一秒存一次,求教一下大家,是怎么回事?
public string str = new ConnString().str;
private void timer2_Tick(object sender, EventArgs e)
{
SqlConnection conn = null;
SqlDataReader rdr = null;
try
{
conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("jiedian1Insert", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pWendu1 =
new SqlParameter("@pwendu1", SqlDbType.VarChar, 10);
pWendu1.Direction = ParameterDirection.Input;
pWendu1.Value = Convert.ToString(wendu1.Text);
cmd.Parameters.Add(pWendu1);
SqlParameter pShidu1 =
new SqlParameter("@pshidu1", SqlDbType.VarChar, 10);
pShidu1.Direction = ParameterDirection.Input;
pShidu1.Value = Convert.ToString(shidu1.Text);
cmd.Parameters.Add(pShidu1);
SqlParameter pGuangzhao1 =
new SqlParameter("@pguangzhao1", SqlDbType.VarChar, 10);
pGuangzhao1.Direction = ParameterDirection.Input;
pGuangzhao1.Value = Convert.ToString(guangzhao1.Text);
cmd.Parameters.Add(pGuangzhao1);
SqlParameter pFire1 =
new SqlParameter("@pfire1", SqlDbType.VarChar, 10);
pFire1.Direction = ParameterDirection.Input;
pFire1.Value = Convert.ToString(fire1.Text);
cmd.Parameters.Add(pFire1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}
USE [zigbee]
GO
/****** Object: StoredProcedure [dbo].[jiedian1Insert] Script Date: 2017/8/15 9:46:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[jiedian1Insert]
@pwendu1 varchar(10),
@pshidu1 varchar(10),
@pguangzhao1 varchar(10),
@pfire1 varchar(10)
AS
BEGIN
INSERT INTO jiedian1(wendu1,shidu1,guangzhao1,fire1)
VALUES(@pwendu1,@pshidu1,@pguangzhao1,@pfire1)
END
GO