2 j1071287319 j1071287319 于 2016.05.07 00:57 提问

关于winform控件添加数据库数据的,一直提示无法将char转换成money,能不能帮忙看看

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 游戏点卡管理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter DataAdapter = new SqlDataAdapter();
    DataSet ds = new DataSet();


    public void ff ()
    {
        try
        {
            dataGridView1.Columns.Clear();
            conn.Open();

            string str = string.Format("select cardid as 卡号,gamename as 游戏名称,company as 发行公司,lastday as 最后有效期,price as 售价 from cardinfo");
            cmd = new SqlCommand(str, conn);
            DataAdapter = new SqlDataAdapter();
            DataAdapter.SelectCommand = cmd;
            ds = new DataSet();

            DataAdapter.Fill(ds, "点卡");
            dataGridView1.DataSource = ds.Tables["点卡"];

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }

        finally
        {
            conn.Close();
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("server=.;pwd=sa;uid=sa;database=GameCardDB;");
        ff();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        string id = this.textBox1.Text.Trim();
        string gs = this.textBox2.Text.Trim();
        int sj = int.Parse(this.textBox3.Text.Trim());
        string mc = this.textBox4.Text.Trim();
        string time = this.dateTimePicker1.Text.Trim();
        conn.Open();
        string str = String.Format("insert into cardinfo values('{0}','{1}',{2},'{3}')", mc, gs, time, sj);
        cmd = new SqlCommand(str, conn);

        try
        {

            int numSQLCount = cmd.ExecuteNonQuery();
            if (numSQLCount > 0)
            {
                MessageBox.Show("添加成功!");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conn.Close();

        }
        ff();

    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
}

}

4个回答

caozhy
caozhy   Ds   Rxr 2016.05.07 05:56

http://blog.csdn.net/superdont/article/details/1695466

你这里的问题是,事件日期需要加上引号,相反,整数不需要。

j1071287319
j1071287319   2016.05.07 13:49

有人吗
,帮忙看看啦

j1071287319
j1071287319   2016.05.07 00:57

create database GameCardDB

on primary
(
name='GameCardDB_data',
filename='d:\GameCardDB_data.mdf',
size=10mb,
maxsize=20mb,
filegrowth=10%
)

log on
(
name='GameCardDB_log',
filename='d:\GameCardDB_log.ldf',
size=10mb,
maxsize=20mb,
filegrowth=10%
)
go

use GameCardDB
go

create table cardinfo
(
cardid int primary key identity(1,1),--卡号
gamename varchar(50) not null,--游戏名称
company varchar(50) not null,--发行公司
lastday datetime not null,--最后有效期
price money not null,--售价
)
go

insert into cardinfo(gamename,company,lastday,price) values('梦想世界','忘了',2016-1-4,50)
insert into cardinfo(gamename,company,lastday,price) values('英雄联盟','拳头',2016-5-7,100)
insert into cardinfo(gamename,company,lastday,price) values('魔兽世界','暴雪',2016-2-3,150)

这是数据库文件

qq_20246395
qq_20246395   2016.05.07 01:28

数据库是money类型,是数值,char是字符。。。转成数字放进去就行了,应该可以是int。float。double。decimal

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!