0

我们在 Matlab 中创建了一个注册表单,其中包含一些字段,例如姓名、年龄等。我们还使用 Sql Server(ODBC) 创建了一个数据库。现在我们可以读取 TextBox 的值并通过 get 属性将其显示在命令窗口中(它在按钮回调中)。我们必须在已经创建的数据库中插入该文本框值我们使用 fastinsert 命令。但它只是手动添加值(使用查询),但我们想通过文本框添加它。我们在按钮回调中的代码在这里。

conn = database('Addface_DSN','sa','123 ');

if(isempty(conn.message))

     disp('database connected')

 else
     disp('cannot connected')

     disp(conn.message);

     return
end

setdbprefs('DataReturnFormat','numeric')

exdata = {'2','Shalu','22','female'};

fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata)

commit(conn)

close(conn);

name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want this name in database.

disp(name)

age = get(handles.edit2, 'string'); 

disp(age)
4

1 回答 1

0

请尝试此解决方案大多数解决方案仅是您的代码

conn = database('Addface_DSN','sa','123 ');

if(isempty(conn.message))

     disp('database connected')

 else
     disp('cannot connected')

     disp(conn.message);

     return
end

setdbprefs('DataReturnFormat','numeric')

// Here is the difference

// Taking test_age as 'int' 
test_name=get(handles.edit1, 'string'); 
test_age= get(handles.edit2, 'string');
//Convert to int and then insert
test_age=str2num(test_age);
exdata2={'3',test_name,test_age,'Male'};

exdata = {'2','Shalu','22','female'};

//Here you insert the data.
fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata2)

commit(conn)

close(conn);

name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want   this name in database.

disp(name)

age = get(handles.edit2, 'string'); 

disp(age)
于 2014-07-21T06:00:07.123 回答