如何从数据库中提取数据到C#变量

时间:2016-01-08 05:10:48

标签: c# sql-server

尝试从select查询中获取c#变量的值时,出现错误

def file_chunks(path):
    with open(path) as fh:
        header = next(fh)
        chunk = []
        prev_id = None

        for line in fh:
            fields = line.split()
            rec_id = fields[0]
            if chunk and rec_id != prev_id:
                yield chunk
                chunk = []
            chunk.append(fields)
            prev_id = rec_id

        if chunk:
            yield chunk

def main():
    records = {}

    for chunk in file_chunks(...):
        # Process the chunk of lines having the same ID.

main()

1 个答案:

答案 0 :(得分:2)

如果要使用ExecuteScalar()从数据库中获取单个值,那么就可以了。

像这样使用;

using (var cmd = new SqlCommand("select group1 from product where productname = @pn)", con))
{
    cmd.Parameters.Add("@pn", SqlDbType.NVarChar).Value = TextBox1.Text;

    object value = cmd.ExecuteScalar();
    data = Convert.ToInt32(value) 
}